Skip to content

Commit

Permalink
Do not send send requests. Instead check whether we receive them
Browse files Browse the repository at this point in the history
  • Loading branch information
ibc committed Feb 19, 2024
1 parent 7ee1753 commit 8ca8895
Show file tree
Hide file tree
Showing 17 changed files with 133 additions and 624 deletions.
47 changes: 4 additions & 43 deletions node/src/WebRtcTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ export type WebRtcTransportOptionsBase<WebRtcTransportAppData> = {

/**
* ICE consent timeout (in seconds). If 0 it is disabled. Default 30.
* For it to be enabled, iceParameters must be given in connect() method.
*/
iceConsentTimeout?: number;

Expand Down Expand Up @@ -476,17 +475,14 @@ export class WebRtcTransport<
* @override
*/
async connect({
iceParameters,
dtlsParameters,
}: {
iceParameters?: IceParameters;
dtlsParameters: DtlsParameters;
}): Promise<void> {
logger.debug('connect()');

const requestOffset = createConnectRequest({
builder: this.channel.bufferBuilder,
iceParameters,
dtlsParameters,
});

Expand Down Expand Up @@ -837,35 +833,18 @@ export function parseWebRtcTransportDumpResponse(

function createConnectRequest({
builder,
iceParameters,
dtlsParameters,
}: {
builder: flatbuffers.Builder;
iceParameters?: IceParameters;
dtlsParameters: DtlsParameters;
}): number {
let iceParametersOffset: number | undefined;

// Serialize IceParameters if given. This can throw.
if (iceParameters) {
iceParametersOffset = serializeIceParameters(builder, iceParameters);
}

// Serialize DtlsParameters. This can throw.
const dtlsParametersOffset = serializeDtlsParameters(builder, dtlsParameters);

const ConnectRequest = FbsWebRtcTransport.ConnectRequest;

// Create request.
ConnectRequest.startConnectRequest(builder);

if (iceParametersOffset) {
ConnectRequest.addIceParameters(builder, iceParametersOffset);
}

ConnectRequest.addDtlsParameters(builder, dtlsParametersOffset);

return ConnectRequest.endConnectRequest(builder);
return FbsWebRtcTransport.ConnectRequest.createConnectRequest(
builder,
dtlsParametersOffset
);
}

function parseGetStatsResponse(
Expand Down Expand Up @@ -934,24 +913,6 @@ function parseDtlsParameters(
};
}

function serializeIceParameters(
builder: flatbuffers.Builder,
iceParameters: IceParameters
): number {
const usernameFragmentOffset = builder.createString(
iceParameters.usernameFragment ?? ''
);
const passwordOffset = builder.createString(iceParameters.password ?? '');
const iceLiteOffset = Boolean(iceParameters.iceLite);

return FbsWebRtcTransport.IceParameters.createIceParameters(
builder,
usernameFragmentOffset,
passwordOffset,
iceLiteOffset
);
}

function serializeDtlsParameters(
builder: flatbuffers.Builder,
dtlsParameters: DtlsParameters
Expand Down
41 changes: 0 additions & 41 deletions node/src/test/test-WebRtcTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,47 +314,6 @@ test('webRtcTransport.connect() succeeds', async () => {
expect(webRtcTransport.dtlsParameters.role).toBe('server');
}, 2000);

test('webRtcTransport.connect() with iceParameters succeeds', async () => {
const webRtcTransport = await ctx.router!.createWebRtcTransport({
listenInfos: [
{ protocol: 'udp', ip: '127.0.0.1', announcedAddress: '9.9.9.1' },
],
});

const iceRemoteParameters: mediasoup.types.IceParameters = {
usernameFragment: 'foo',
password: 'xxxx',
};

const dtlsRemoteParameters: mediasoup.types.DtlsParameters = {
fingerprints: [
{
algorithm: 'sha-256',
value:
'82:5A:68:3D:36:C3:0A:DE:AF:E7:32:43:D2:88:83:57:AC:2D:65:E5:80:C4:B6:FB:AF:1A:A0:21:9F:6D:0C:AD',
},
],
role: 'client',
};

await expect(
webRtcTransport.connect({
iceParameters: iceRemoteParameters,
dtlsParameters: dtlsRemoteParameters,
})
).resolves.toBeUndefined();

// Must fail if connected.
await expect(
webRtcTransport.connect({
iceParameters: iceRemoteParameters,
dtlsParameters: dtlsRemoteParameters,
})
).rejects.toThrow(Error);

expect(webRtcTransport.dtlsParameters.role).toBe('server');
}, 2000);

test('webRtcTransport.connect() with wrong arguments rejects with TypeError', async () => {
const webRtcTransport = await ctx.router!.createWebRtcTransport({
listenInfos: [
Expand Down
10 changes: 2 additions & 8 deletions rust/examples/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,7 @@ impl Handler<ClientMessage> for EchoConnection {
// synchronous
actix::spawn(async move {
match transport
.connect(WebRtcTransportRemoteParameters {
ice_parameters: None,
dtls_parameters,
})
.connect(WebRtcTransportRemoteParameters { dtls_parameters })
.await
{
Ok(_) => {
Expand Down Expand Up @@ -349,10 +346,7 @@ impl Handler<ClientMessage> for EchoConnection {
// The same as producer transport, but for consumer transport
actix::spawn(async move {
match transport
.connect(WebRtcTransportRemoteParameters {
ice_parameters: None,
dtls_parameters,
})
.connect(WebRtcTransportRemoteParameters { dtls_parameters })
.await
{
Ok(_) => {
Expand Down
5 changes: 1 addition & 4 deletions rust/examples/multiopus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,7 @@ impl Handler<ClientMessage> for EchoConnection {
// The same as producer transport, but for consumer transport
actix::spawn(async move {
match transport
.connect(WebRtcTransportRemoteParameters {
ice_parameters: None,
dtls_parameters,
})
.connect(WebRtcTransportRemoteParameters { dtls_parameters })
.await
{
Ok(_) => {
Expand Down
10 changes: 2 additions & 8 deletions rust/examples/svc-simulcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,7 @@ impl Handler<ClientMessage> for SvcSimulcastConnection {
// synchronous
actix::spawn(async move {
match transport
.connect(WebRtcTransportRemoteParameters {
ice_parameters: None,
dtls_parameters,
})
.connect(WebRtcTransportRemoteParameters { dtls_parameters })
.await
{
Ok(_) => {
Expand Down Expand Up @@ -395,10 +392,7 @@ impl Handler<ClientMessage> for SvcSimulcastConnection {
// The same as producer transport, but for consumer transport
actix::spawn(async move {
match transport
.connect(WebRtcTransportRemoteParameters {
ice_parameters: None,
dtls_parameters,
})
.connect(WebRtcTransportRemoteParameters { dtls_parameters })
.await
{
Ok(_) => {
Expand Down
10 changes: 2 additions & 8 deletions rust/examples/videoroom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,10 +659,7 @@ mod participant {
// synchronous
actix::spawn(async move {
match transport
.connect(WebRtcTransportRemoteParameters {
ice_parameters: None,
dtls_parameters,
})
.connect(WebRtcTransportRemoteParameters { dtls_parameters })
.await
{
Ok(_) => {
Expand Down Expand Up @@ -721,10 +718,7 @@ mod participant {
// The same as producer transport, but for consumer transport
actix::spawn(async move {
match transport
.connect(WebRtcTransportRemoteParameters {
ice_parameters: None,
dtls_parameters,
})
.connect(WebRtcTransportRemoteParameters { dtls_parameters })
.await
{
Ok(_) => {
Expand Down
8 changes: 0 additions & 8 deletions rust/src/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,6 @@ pub struct IceParameters {
}

impl IceParameters {
pub(crate) fn to_fbs(&self) -> web_rtc_transport::IceParameters {
web_rtc_transport::IceParameters {
username_fragment: self.username_fragment.to_string(),
password: self.password.to_string(),
ice_lite: self.ice_lite.unwrap_or(false),
}
}

pub(crate) fn from_fbs(parameters: web_rtc_transport::IceParameters) -> Self {
Self {
username_fragment: parameters.username_fragment.to_string(),
Expand Down
8 changes: 2 additions & 6 deletions rust/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1408,7 +1408,6 @@ pub(crate) struct WebRtcTransportConnectResponse {

#[derive(Debug)]
pub(crate) struct WebRtcTransportConnectRequest {
pub(crate) ice_parameters: Option<IceParameters>,
pub(crate) dtls_parameters: DtlsParameters,
}

Expand All @@ -1419,11 +1418,8 @@ impl Request for WebRtcTransportConnectRequest {

fn into_bytes(self, id: u32, handler_id: Self::HandlerId) -> Vec<u8> {
let mut builder = Builder::new();
let data = web_rtc_transport::ConnectRequest::create(
&mut builder,
self.ice_parameters.map(|parameters| parameters.to_fbs()),
self.dtls_parameters.to_fbs(),
);
let data =
web_rtc_transport::ConnectRequest::create(&mut builder, self.dtls_parameters.to_fbs());
let request_body =
request::Body::create_web_rtc_transport_connect_request(&mut builder, data);
let request = request::Request::create(
Expand Down
11 changes: 1 addition & 10 deletions rust/src/router/webrtc_transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ pub struct WebRtcTransportOptions {
/// Default false.
pub prefer_tcp: bool,
/// ICE consent timeout (in seconds). If 0 it is disabled.
/// For it to be enabled, iceParameters must be given in connect() method.
/// Default 30.
pub ice_consent_timeout: u8,
/// Create a SCTP association.
Expand Down Expand Up @@ -390,8 +389,6 @@ impl WebRtcTransportStat {
#[derive(Debug, Clone, PartialOrd, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct WebRtcTransportRemoteParameters {
/// Remote ICE parameters.
pub ice_parameters: Option<IceParameters>,
/// Remote DTLS parameters.
pub dtls_parameters: DtlsParameters,
}
Expand Down Expand Up @@ -928,7 +925,7 @@ impl WebRtcTransport {
///
/// # Example
/// ```rust
/// use mediasoup::data_structures::{IceParameters, DtlsParameters, DtlsRole, DtlsFingerprint};
/// use mediasoup::data_structures::{DtlsParameters, DtlsRole, DtlsFingerprint};
/// use mediasoup::webrtc_transport::WebRtcTransportRemoteParameters;
///
/// # async fn f(
Expand All @@ -937,11 +934,6 @@ impl WebRtcTransport {
/// // Calling connect() on a PlainTransport created with comedia and rtcp_mux set.
/// webrtc_transport
/// .connect(WebRtcTransportRemoteParameters {
/// ice_parameters: Some(IceParameters {
/// username_fragment: "foo".to_string(),
/// password: "xxxx".to_string(),
/// ice_lite: None,
/// }),
/// dtls_parameters: DtlsParameters {
/// role: DtlsRole::Server,
/// fingerprints: vec![
Expand Down Expand Up @@ -971,7 +963,6 @@ impl WebRtcTransport {
.request(
self.id(),
WebRtcTransportConnectRequest {
ice_parameters: remote_parameters.ice_parameters,
dtls_parameters: remote_parameters.dtls_parameters,
},
)
Expand Down
70 changes: 3 additions & 67 deletions rust/tests/integration/webrtc_transport.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use futures_lite::future;
use hash_hasher::HashedSet;
use mediasoup::data_structures::{
AppData, DtlsFingerprint, DtlsParameters, DtlsRole, DtlsState, IceCandidateType, IceParameters,
IceRole, IceState, ListenInfo, Protocol, SctpState,
AppData, DtlsFingerprint, DtlsParameters, DtlsRole, DtlsState, IceCandidateType, IceRole,
IceState, ListenInfo, Protocol, SctpState,
};
use mediasoup::prelude::*;
use mediasoup::router::{Router, RouterOptions};
Expand Down Expand Up @@ -418,7 +418,6 @@ fn connect_succeeds() {

transport
.connect(WebRtcTransportRemoteParameters {
ice_parameters: None,
dtls_parameters: dtls_parameters.clone(),
})
.await
Expand All @@ -427,70 +426,7 @@ fn connect_succeeds() {
// Must fail if connected.
assert!(matches!(
transport
.connect(WebRtcTransportRemoteParameters {
ice_parameters: None,
dtls_parameters
})
.await,
Err(RequestError::Response { .. }),
));

assert_eq!(transport.dtls_parameters().role, DtlsRole::Server);
});
}

#[test]
fn connect_with_ice_parameters_succeeds() {
future::block_on(async move {
let (_worker, router) = init().await;

let transport = router
.create_webrtc_transport(WebRtcTransportOptions::new(
WebRtcTransportListenInfos::new(ListenInfo {
protocol: Protocol::Udp,
ip: IpAddr::V4(Ipv4Addr::LOCALHOST),
announced_address: Some("9.9.9.1".to_string()),
port: None,
flags: None,
send_buffer_size: None,
recv_buffer_size: None,
}),
))
.await
.expect("Failed to create WebRTC transport");

let ice_parameters = IceParameters {
username_fragment: "foo".to_string(),
password: "xxxx".to_string(),
ice_lite: None,
};

let dtls_parameters = DtlsParameters {
role: DtlsRole::Client,
fingerprints: vec![DtlsFingerprint::Sha256 {
value: [
0x82, 0x5A, 0x68, 0x3D, 0x36, 0xC3, 0x0A, 0xDE, 0xAF, 0xE7, 0x32, 0x43, 0xD2,
0x88, 0x83, 0x57, 0xAC, 0x2D, 0x65, 0xE5, 0x80, 0xC4, 0xB6, 0xFB, 0xAF, 0x1A,
0xA0, 0x21, 0x9F, 0x6D, 0x0C, 0xAD,
],
}],
};

transport
.connect(WebRtcTransportRemoteParameters {
ice_parameters: Some(ice_parameters.clone()),
dtls_parameters: dtls_parameters.clone(),
})
.await
.expect("Failed to establish WebRTC transport connection");

// Must fail if connected.
assert!(matches!(
transport
.connect(WebRtcTransportRemoteParameters {
ice_parameters: Some(ice_parameters),
dtls_parameters
})
.connect(WebRtcTransportRemoteParameters { dtls_parameters })
.await,
Err(RequestError::Response { .. }),
));
Expand Down
Loading

0 comments on commit 8ca8895

Please sign in to comment.