From cb1a14557bd0f3a7791bac6238648838e449ee5a Mon Sep 17 00:00:00 2001 From: rtm516 Date: Wed, 21 Aug 2024 15:07:24 +0100 Subject: [PATCH] Use ephemeral ports for the ICE data stream --- .../core/webrtc/PeerSession.java | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/com/rtm516/mcxboxbroadcast/core/webrtc/PeerSession.java b/core/src/main/java/com/rtm516/mcxboxbroadcast/core/webrtc/PeerSession.java index 4ea36f3..4b20944 100644 --- a/core/src/main/java/com/rtm516/mcxboxbroadcast/core/webrtc/PeerSession.java +++ b/core/src/main/java/com/rtm516/mcxboxbroadcast/core/webrtc/PeerSession.java @@ -70,7 +70,7 @@ public void receiveOffer(BigInteger from, String sessionId, String message) { } } - component = agent.createComponent(stream, 5000, 5000, 6000, KeepAliveStrategy.SELECTED_ONLY, true); + component = agent.createComponent(stream, KeepAliveStrategy.SELECTED_ONLY, true); var transport = new CustomDatagramTransport(); @@ -93,7 +93,7 @@ public void receiveOffer(BigInteger from, String sessionId, String message) { media.setAttribute("fingerprint", "sha-256 " + client.getClientFingerprint()); media.setAttribute("setup", "active"); media.setAttribute("mid", "0"); - media.setAttribute("sctp-port", "5000"); + media.setAttribute("sctp-port", String.valueOf(getComponentPort(5000))); media.setAttribute("max-message-size", "262144"); answer.setMediaDescriptions(new Vector<>(Collections.of(media))); @@ -194,4 +194,22 @@ private RemoteCandidate parseCandidate(String value, IceMediaStream stream) { return new RemoteCandidate(transAddr, component, type, foundation, priority, relatedCandidate, ufrag); } + + /** + * Get the port of the first host candidate. + * + * @param fallback the port to return if no host candidate is found + * @return the port of the first host candidate or the fallback port + */ + private int getComponentPort(int fallback) { + int port = fallback; + for (LocalCandidate localCandidate : component.getLocalCandidates()) { + if (localCandidate.getType() == CandidateType.HOST_CANDIDATE) { + port = localCandidate.getTransportAddress().getPort(); + break; + } + } + + return port; + } }