Skip to content

Commit

Permalink
Initiate the socket
Browse files Browse the repository at this point in the history
  • Loading branch information
rtm516 committed Aug 19, 2024
1 parent 5a4372d commit aa357b0
Showing 1 changed file with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.ice4j.ice.CandidateType;
import org.ice4j.ice.Component;
import org.ice4j.ice.IceMediaStream;
import org.ice4j.ice.IceProcessingState;
import org.ice4j.ice.RemoteCandidate;
import org.ice4j.ice.harvest.StunCandidateHarvester;
import org.ice4j.ice.harvest.TurnCandidateHarvester;
Expand Down Expand Up @@ -145,8 +146,8 @@ private void handleConnectRequest(BigInteger from, String sessionId, String mess

var offer = factory.createSessionDescription(message);

String userFragment;
String password;
String userFragment = "";
String password = "";
String fingerprint;
for (Object mediaDescription : offer.getMediaDescriptions(false)) {
var description = (MediaDescription) mediaDescription;
Expand All @@ -166,6 +167,9 @@ private void handleConnectRequest(BigInteger from, String sessionId, String mess
}
}

component.getParentStream().setRemoteUfrag(userFragment);
component.getParentStream().setRemotePassword(password);

var keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048);
var keyPair = keyPairGenerator.generateKeyPair();
Expand Down Expand Up @@ -210,6 +214,16 @@ protected ProtocolVersion[] getSupportedVersions() {
};

CustomDatagramTransport datagramTransport = new CustomDatagramTransport(component);
agent.addStateChangeListener(evt -> {
if (evt.getPropertyName().equals("IceProcessingState") && evt.getNewValue().equals(IceProcessingState.COMPLETED)) {
System.out.println("ICE processing completed, starting DTLS handshake");
try {
new DTLSClientProtocol().connect(client, datagramTransport);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});

var answer = factory.createSessionDescription();
long answerSessionId = new Random().nextLong();
Expand Down Expand Up @@ -246,8 +260,14 @@ protected ProtocolVersion[] getSupportedVersions() {
send(jsonAdd);
});

// Move this since it errors since the socket isnt open, I assume bc we havent sent the CANDIDATEADD responses
// new DTLSClientProtocol().connect(client, datagramTransport);
new Thread(() -> {
try {
Thread.sleep(2_500);
agent.startConnectivityEstablishment();
} catch (Exception e) {
e.printStackTrace();
}
}).start();

// } catch (SdpException | FileNotFoundException | CertificateException | NoSuchAlgorithmException e) {
} catch (Exception e) {
Expand Down Expand Up @@ -338,9 +358,12 @@ private void initialize(JsonObject message) {
// pendingSession = new PeerSession(this, rtcConfig);

agent = new Agent();
agent.addStateChangeListener(evt -> {
logger.info("ICE Agent state changed: " + evt);
});

try {
IceMediaStream stream = agent.createMediaStream("data");
IceMediaStream stream = agent.createMediaStream("rtcmedia");
component = agent.createComponent(stream, 5000, 5000, 6000);
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down

0 comments on commit aa357b0

Please sign in to comment.