webrtc-java
webrtc-java copied to clipboard
Can't open DataChannel with a JavaScript client
Describe the bug I am trying to replicate a JS example using this library following the exact same steps, I can generate and assign both offer and answer but the connection is not established.
To Reproduce Steps to reproduce the behavior:
- Replace the following SDP_OFFER constant by a real offer from the first peer.
- Execute the code
- Assign the answer to the first peer (A)
- The channel is not open even after setting the local description on the first peer (A)
Expected behavior Channel should be open
Desktop (please complete the following information):
- OS: Fedora Linux
- Version 32
Additional context The code works with two JavaScript peers but not when peerB is Java code.
Original working code (JavaScript): https://github.com/hnasr/javascript_playground/tree/master/webrtc
import dev.onvoid.webrtc.*;
public class WebRTCRemote {
public static final String SDP_OFFER = "";
private static RTCPeerConnection remoteConnection;
public static void main(String[] args) {
RTCConfiguration configuration = new RTCConfiguration();
// Init connection
remoteConnection = new PeerConnectionFactory().createPeerConnection(configuration, new PeerConnectionObserver() {
@Override
public void onIceCandidate(RTCIceCandidate rtcIceCandidate) {
System.out.println("Desktop NEW ice candidnat!! on localconnection reprinting SDP ");
String json = descriptionAsJson(remoteConnection.getLocalDescription());
System.out.println(json);
}
@Override
public void onDataChannel(RTCDataChannel dataChannel) {
RTCDataChannel receiveChannel = dataChannel;
receiveChannel.registerObserver(new RTCDataChannelObserver() {
@Override
public void onMessage(RTCDataChannelBuffer rtcDataChannelBuffer) {
// Turn into string
System.out.println("message received!!!" + rtcDataChannelBuffer.data);
}
@Override
public void onBufferedAmountChange(long l) {
}
@Override
public void onStateChange() {
if (RTCDataChannelState.OPEN == receiveChannel.getState()) {
System.out.println("open!!!!");
} else if (RTCDataChannelState.OPEN == receiveChannel.getState()) {
System.out.println("closed!!!!");
}
}
});
}
});
RTCSessionDescription offer = new RTCSessionDescription(RTCSdpType.OFFER, SDP_OFFER);
remoteConnection.setRemoteDescription(offer, new SetSessionDescriptionObserver() {
@Override
public void onSuccess() {
System.out.println("done");
}
@Override
public void onFailure(String s) {
}
});
// Create Answer
RTCAnswerOptions answerOptions = new RTCAnswerOptions();
remoteConnection.createAnswer(answerOptions, new CreateSessionDescriptionObserver() {
@Override
public void onSuccess(RTCSessionDescription rtcSessionDescription) {
remoteConnection.setLocalDescription(rtcSessionDescription, new SetSessionDescriptionObserver() {
@Override
public void onSuccess() {
String json = descriptionAsJson(remoteConnection.getLocalDescription());
System.out.println(json);
}
@Override
public void onFailure(String s) {
}
});
}
@Override
public void onFailure(String s) {
}
});
}
public static String descriptionAsJson(RTCSessionDescription sessionDescription) {
StringBuilder builder = new StringBuilder();
builder.append("{");
builder.append("\"type\":\"");
builder.append(sessionDescription.sdpType.name().toLowerCase());
builder.append("\",");
builder.append("\"sdp\":\"");
String sdp = sessionDescription.sdp;
sdp = sdp.replaceAll("\r\n", "\\\\r\\\\n");
builder.append(sdp);
builder.append("\"}");
return builder.toString();
}
}