devopvoid / webrtc-java

WebRTC for desktop platforms running Java
Apache License 2.0
248 stars 60 forks source link

Can't open DataChannel with a JavaScript client #66

Closed yuripourre closed 1 year ago

yuripourre commented 2 years ago

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:

  1. Replace the following SDP_OFFER constant by a real offer from the first peer.
  2. Execute the code
  3. Assign the answer to the first peer (A)
  4. 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):

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();
    }

}
devopvoid commented 1 year ago

Hi, in your example...

        remoteConnection.setRemoteDescription(offer, new SetSessionDescriptionObserver() {
            @Override
            public void onSuccess() {
                System.out.println("done");

              // Place here your logic to create the answer:
              RTCAnswerOptions answerOptions = new RTCAnswerOptions();
              remoteConnection.createAnswer(answerOptions, new CreateSessionDescriptionObserver() {
                .......
            }

            @Override
            public void onFailure(String s) {

            }
        });