How can I receive a notification/event when mediaStreamTrack.enabled is modified?

2.2k Views Asked by At

I am using getUserMedia to get access to web cam. I have a function which toggle on and off the video doing the following:

  var videoTracks = this.stream.getVideoTracks();
  if (videoTracks.length === 0) {
    trace('No local video available.');
    return;
  }

  trace('Toggling video mute state.');
  for (var i = 0; i < videoTracks.length; ++i) {
    videoTracks[i].enabled = !videoTracks[i].enabled;
  }

  trace('Video ' + (videoTracks[0].enabled ? 'unmuted.' : 'muted.'));

How can receive an event when the the value of enabled is changed? I tried to use Object.observe, but it doesn't work.

1

There are 1 best solutions below

0
On BEST ANSWER

As far as I can tell there currently is no event fired/callback invoked when the enabled property changes.

From here:

Also, there is no "onmuted" and "onunmuted" event defined or fired in the WebRTC native implementations.

You might have to build this mechanism yourself:

Keep in mind that you're disabling a media track locally; it will not fire any event on target users side. If you disabled video track; then it will cause "blank-video" on target-users side.

You can manually fire events like "onmuted" or "onmediatrackdisabled" by using socket that was used for signaling. You can send/emit messages like:

yourSignalingSocket.send({
    isMediaStreamTrackDisabled: true,
    mediaStreamLabel: stream.label
});

According to the spec this should be part of the MediaStreamTrack interface eventually:

onmute of type EventHandler,

This event handler, of type mute, must be supported by all objects implementing the MediaStreamTrack interface.

I tried assigning a function to a track's onmute in Chrome (43) but it never got called (looks like this is not implemented yet).