webRTC java desktop attach media tracks and consume them correctly
I’m trying to build a peer2peer audio transmission between two java apps, using this webRTC Java native interface implementation, and using a simple nodejs server with sockets to do the signaling, I could establish a connection although I couldn’t get the audio tracks to work, tried adding tracks, adding transceivers, passing the audioDeviceModule to the PeerConnectionFactory, nothing has worked, I just want to attach the microphone track from the caller and have it play properly on the other side.
the demo provided on their repo is complicated and It didn’t help me understand the API, a simple "getting started" example would be helpful.
what I have so far: the structure is basic I’m running two java instances, sending an offer from the first instance to the server through the socket, the server forwards it to the second instance, which sets the remote description and sends the answer to the server, the server forwards it to the first instance, which sets the remote description
Caller:
connection.createOffer(options, new CreateSessionDescriptionObserver() { @Override public void onSuccess(RTCSessionDescription description) { connection.setLocalDescription(description, new SetSessionDescriptionObserver() { @Override public void onSuccess() { System.out.println("caller ready"); } @Override public void onFailure(String error) { System.out.println("failed to set local description : " + error); } }); //Sending offer as a json object to the server JSONObject data = new JSONObject(); JSONObject offer = new JSONObject(); offer.put("sdp", description.sdp); offer.put("type", description.sdpType); data.put("offer", offer); data.put("to", socketId); mySocket.emit("call-user", data.toString()); System.out.println("offer sent to " + socketId); //Listening for answer events mySocket.on("answer-made", e -> { JSONObject obj = (JSONObject) e[0]; String sdp = obj.getJSONObject("answer").getString("sdp"); System.out.println("received answer from " + obj.getString("socket")); connection.setRemoteDescription(new RTCSessionDescription(RTCSdpType.ANSWER, sdp), new SetSessionDescriptionObserver() { @Override public void onSuccess() { System.out.println("Connection Successful"); } @Override public void onFailure(String error) { System.out.println("failed to set remote description : " + error); } }); }); } @Override public void onFailure(String error) { System.out.println("failed to create offer: " + error); } });
Callee:
socket.on("call-made", e -> { JSONObject obj = (JSONObject) e[0]; String sdp = obj.getJSONObject("offer").getString("sdp"); String from = obj.getString("socket"); System.out.println("received offer from " + from); RTCSessionDescription offer = new RTCSessionDescription(RTCSdpType.OFFER, sdp); connection.setRemoteDescription(offer, new SetSessionDescriptionObserver() { @Override public void onSuccess() { connection.createAnswer(options, new CreateSessionDescriptionObserver() { @Override public void onSuccess(RTCSessionDescription description) { connection.setLocalDescription(description, new SetSessionDescriptionObserver() { @Override public void onSuccess() { JSONObject data = new JSONObject(); JSONObject answer = new JSONObject(); answer.put("sdp", description.sdp); answer.put("type", description.sdpType); data.put("answer", answer); data.put("to", socketId); System.out.println("answer sent to " + socketId); mySocket.emit("make-answer", data.toString()); System.out.println("Peer2Peer Connection Successful"); } @Override public void onFailure(String error) { System.out.println("failed to set local description : " + error); } }); } @Override public void onFailure(String error) { System.out.println("failed to create answer : " + error); } }); } @Override public void onFailure(String error) { System.out.println("failed to set remote description : " + error); } }); });
the server does nothing but forwards the offer/answer to the other socket
the connection is being set correctly and I’m getting the following outputs with no failures
Caller:
connecting to server... connected, connection id : 7rO5KeRKQQjmGBiRAAAB offer enter id to call nVlQRjwWsoPrM63uAAAA offer sent to nVlQRjwWsoPrM63uAAAA caller ready received answer from nVlQRjwWsoPrM63uAAAA Peer2Peer Connection Successful
Callee:
connecting to server... connected, connection id : nVlQRjwWsoPrM63uAAAA received offer from 7rO5KeRKQQjmGBiRAAAB answer sent to 7rO5KeRKQQjmGBiRAAAB Peer2Peer Connection Successful