Better support custom MediaStreams in openvidu-browser

TLDR; To help detecting disposal of local MediaStreamTracks fire an "ended" event every time openvidu-browser calls track.stop().

With Custom MediaStreams I mean such which are created (navigator.mediaDevices.getUserMedia()) and disposed locally by my browser application or the local operating system.

Usually, I hand the MediaStreamTracks of my Custom MediaStream to OpenVidu by OpenVidu.initPublisher(null, {audioSource: audioTrack, videoSource: videoTrack});. So far so good.

Sometimes OpenVidu disposes my local tracks (e.g. in Stream.disposeMediaStream()) and that’s fine too. However in these cases I need to indirectly detect the disposal by subscribing for "streamDestroyed" or other Publisher events. (Reason is that track.stop() does not fire "ended"). That indirect detection process adds IMO unnecessary dependencies in my code.

Also discussed on SOW, I find that the standard is flawed:

  • Sure I know when I my code calls track.stop(), but that can be in many places. Wiring cleanup code every time is no good style. Better use events to support loose coupling and write the cleanup code once.
  • The standard clearly fails if I hand my MediaStreamTracks to a library (like OpenVidu) which then has the liberty to track.stop() without me getting a notification for that.

Do you see any danger if openvidu-browser adds a track.dispatchEvent(new Event("ended")) to track.stop()?

If not, I’d file a PR.

Btw. Firefox has a bug in their MediaStreamTrack implementation as an EventTarget so that you cannot use track.addEventListener("ended", …) to handle track.dispatchEvent(new Event("ended")) events. You’ll need to work around that with track.onended.