Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MediaStream events active and inactive are not triggered in a remote WebRTC Peer Connection

I have a remote MediaStream object obtained by a remote WebRTC Peer Connection.

I want to check when the remote MediaStream becomes inactive (indipendently by the reason).

I have read that for this purpose I should use the events active and inactive of the MediaStream object.

But these two events are never triggered: even if I set a specific handler for these two events, the handlers are never executed.

Here my implementation:

function onRemoteStream(event) {
    event.stream.addEventListener("active", function(){
    console.log('The video is active');
    }, false);
    event.stream.addEventListener("inactive", function(){
        console.log('The video is not active');
    }, false);
    remoteVideo.src = window.URL.createObjectURL(event.stream);
}

The two messages are never showed.

I also tried:

function onRemoteStream(event) {
    event.stream.onactive = function(){
        console.log('The video is active');
    };
    event.stream.oninactive = function(){
        console.log('The video is not active');
    }
    remoteVideo.src = window.URL.createObjectURL(event.stream);
}

But the behaviour is the same.

I don't understand why the two events are not triggered.

I'm using Google Chrome 52.0.2743.116 m

like image 580
Alessandro C Avatar asked Aug 24 '16 10:08

Alessandro C


1 Answers

No it is working properly, it is just that you cannot directly write a function for that event (As per the documentation which is now removed). You can also log your event.stream object in console and see the details. You have to use predefined properties onactive and oninactive

I have made a quick code to test how to trigger these events.

HTML

<video src="" autoplay id="video"></video>

JavaScript

var video = document.getElementById('video')

function onactive() {
  console.log("on active event");
}

function oninactive() {
  console.log("on inactive event");
}

navigator.getUserMedia({video:true, audio:false}, function (stream) {
  stream.onactive = onactive;
  stream.oninactive = oninactive;

  video.src = window.URL.createObjectURL(stream);
}, function (error) {
  console.log(error);
})
like image 119
Apal Shah Avatar answered Nov 09 '22 01:11

Apal Shah