MenuMenu

RTCMultiConnection.setStreamEndHandler

This method fires callback as soon as any of the stream track is ended/stopped

How to use

connection.setStreamEndHandler(screenStream, null, function() {
    alert('Screen stream is stopped. It is automatically removed from all connected users side.')
});

Alternative

addStreamStopListener(screenStream, function() {
    alert('screen sharing is ended.');

    // remove from RTCPeerConnectino objects
    // and renegotiate
    // or resetTrack
});

function addStreamStopListener(stream, callback) {
    var streamEndedEvent = 'ended';
    if ('oninactive' in stream) {
        streamEndedEvent = 'inactive';
    }
    stream.addEventListener(streamEndedEvent, function() {
        callback();
        callback = function() {};
    }, false);
    stream.getAudioTracks().forEach(function(track) {
        track.addEventListener(streamEndedEvent, function() {
            callback();
            callback = function() {};
        }, false);
    });
    stream.getVideoTracks().forEach(function(track) {
        track.addEventListener(streamEndedEvent, function() {
            callback();
            callback = function() {};
        }, false);
    });
}