0

Currently, streaming video from one client (streamer) to another (viewers) using the program has been implemented. Everything works well until the viewer reloads the page. His video disappears, although the stream was transmitted (this can be seen in the console). What is the problem?

Streamer.js

useEffect(() => {
    navigator.mediaDevices
        .getUserMedia({
            video: true,
            audio: true,
        })
        .then(stream => {
            // Display own video
            localVideo.current.srcObject = stream;

            // Stream video to attendees
            gotMedia(stream);
        })
        .catch(error => {
            console.error(error);
        });
}, []);

const gotMedia = stream => {
    localStream.current = stream;
    broadcaster.current = new Peer({
        initiator: true,
        stream: localStream.current,
        config: configuration,
        offerConstraints: {
            offerToReceiveAudio: true,
            offerToReceiveVideo: true,
        },
    });
    broadcaster.current.on('signal', data => {
        socket.emit('signal', { data, room: routeMatch.params.id });
    });
    // destroy current peerconnection and create a new one
    socket.on('startConnection', () => {
        console.log('startConnection');
        if (broadcaster.current) broadcaster.current.destroy();
        broadcaster.current = new Peer({
            initiator: true,
            stream: localStream.current,
            config: configuration,
            offerConstraints: {
                offerToReceiveAudio: true,
                offerToReceiveVideo: true,
            },
        });
        broadcaster.current.on('signal', data => {
            socket.emit('signal', { data, room: routeMatch.params.id });
        });
    });
    socket.on('signal', data => {
        broadcaster.current && broadcaster.current.signal(data);
    });
};

Viewer.js

useEffect(() => {
    attendee.current = new Peer({
        initiator: false,
        config: configuration,
        answerConstraints: {
            offerToReceiveAudio: false,
            offerToReceiveVideo: false,
        },
    });
    attendee.current.on('signal', data => {
        socket.emit('signal', { data, room: routeMatch.params.id });
    });

    socket.on('signal', data => {
        attendee.current && attendee.current.signal(data);
    });

    // Get remote video stream and display it
    attendee.current.on('stream', stream => {
        console.log('stream', stream);
        console.log('remoteVideo.current.srcObject before', remoteVideo.current.srcObject);
        remoteVideo.current.srcObject = stream;
        console.log('remoteVideo.current.srcObject after', remoteVideo.current.srcObject);
    });

    // Ask broadcaster to start his connection
    socket.emit('startConnection', routeMatch.params.id);

    return () => {
        attendee.current.destroy();
    };
}, []);

After reloading the page, we see that there is a stream, but for some reason the video does not play (console.log('remoteVideo.current.srcObject after', remoteVideo.current.srcObject); )

1 Answers1

1

The problem is that when you initialize your Peer in streamer with ìnitialize: true, then it will try to connect directly with the given settings. When you reload your viewer, then it will not receive any signal because the streamer sends this initialization once.

The solution for this would be to create a new Peer object in the streamer as soon as a viewer connects. (and also remove that object when the viewer disconnects).

Here is a github project (demo) you can use as inspiration to expand your project.

Dirk V
  • 1,373
  • 12
  • 24
  • I do so, as you advised. As soon as the viewer connects, I destoy the current peer and create a new one in the method `gotMedia` after comment `// destroy current peerconnection and create a new one` – ForeverJunior Jun 09 '20 at 05:23
  • No:( After reload page video does not play – ForeverJunior Jun 10 '20 at 15:26
  • @zXus look at [this](https://github.com/Dirvann/webrtc-video-conference-simple-peer/blob/master/public/js/main.js#L110) how to remove it. I can't see your code now so hard to say what you did wrong. – Dirk V Jun 10 '20 at 18:06
  • @zXus If your problem is solved you can mok the answer as solved – Dirk V Jun 13 '20 at 10:43
  • @DirkV can you suggest to me how to add room functionality to the above GitHub demo? As in that demo, it simply starts streaming on page load, how to specify room name to it. – Rajan Aug 04 '22 at 21:03