devopvoid / webrtc-java

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

[Question] Connecting media tracks between two peer connections #32

Closed Charles92011 closed 2 years ago

Charles92011 commented 2 years ago

I'm using JavaRTC to create a simple server that is connected to by web page clients. In essence, each web page creates an RTC Peer Connection and connects to the Java-RTC app, exchanging SDPs and IceCandidates using a web socket.
The JavaRTC app connects successfully to both web page clients, verified by sending messages through the data channel.

Each web client is using one track of media - a video stream.
Within the JavaRTC I'm attempting to connect the inbound video stream of one PeerConnection with the outbound of the other.

In the web page it should trigger a track event, or renegotiation, but neither happens

These code snippets are pretty simplistic, but they demonstrate what I'm trying to do.

Java RTC

public void watch(RTCPeerConnection peerConnection, RTCPeerConnection connectionToWatch) {

    final int receivers = connectionToWatch.getReceivers().length;

    if (receivers == 0) return;

    for (int index = 0; index < receivers; index++) {

        final RTCRtpReceiver receiver = connectionToWatch.getReceivers()[index];
        if (receiver != null) {

            final MediaStreamTrack track = receiver.getTrack();
            if (track != null) {

                System.out.printf("Adding track: %s\n", track.getKind());

                List<String> streamIds = new ArrayList<String>();
                streamIds.add("0");

                @SuppressWarnings("unused")
                RTCRtpSender sender = peerConnection.addTrack(track, streamIds);
            }
        }
    }
}

Web page:

const peerConnection = new RTCPeerConnection(servers);
peerConnection.addEventListener('track', onTrack);

function onTrack(event)
{
    console.log('Track');

    const remoteStream = new MediaStream();
    const remoteVideo = document.createElement('video');

    remoteVideos.appendChild(remoteVideo);

    remoteVideo.autoplay = true;
    remoteVideo.playsinline = true;

    remoteStream.addTrack(event.track);

    remoteVideo.srcObject = mediaStream;
}

Am I doing something wrong?