1

I am creating multiple webrtc peer connections and creating a single mediastream using

if (mediaStream == undefined) {
            navigator.mediaDevices.getUserMedia({
                audio: true,
                video: true
            }).then(function (stream) {
                mediaStream = stream;
                mediaStream.getTracks().forEach(function (track) {
                    rtcPeerConns[userName].addTrack(track, mediaStream);
                });
            }).catch(function (err) {
                console.log("get user media " + err.name + ": " + err.message);
            });
        } else {
            console.log("using the existing local stream");
            mediaStream.getTracks().forEach(function (track) {
                rtcPeerConns[userName].addTrack(track, mediaStream);
            });
        }

Everything works perfectly until the last peer connection is closed and I want to close the mediastream.

if (mediaStream != undefined) {
        if (mediaStream.active) {
            mediaStream.getTracks().forEach(function (track) {
                track.stop();
            });
            mediaStream = null;
        }
    }

If only 1 peer connection has been used then everything shuts down as planned. If more than 1 peer connection has used the MediaStream then the MediaStream becomes null , but the camera indicator on the browser and the camera light both stay on.

What am I missing?

mkUltra
  • 2,828
  • 1
  • 22
  • 47
Croftie
  • 13
  • 4
  • refactored the code and now embarrassingly It works. In the full code there were 2 places where the mediastream could be called depending on whether the client was the offeror or the offeree. One of them was calling a new media stream and that was the problem. Thanks to Kaiido for perseverance – Croftie Nov 25 '18 at 10:59

1 Answers1

3

That's a bit of a guess, but seems like the most possible reason, so while having more code would help...

If you do enter that first code block

if (mediaStream == undefined) {
    navigator.mediaDevices.getUserMedia({
      ...

again before the first Promise returned by getUserMedia gets resolved, then you will have actually multiple different MediaStreams coming from your device.

The global variable mediaStream will only represent the last MediaStream got from getUserMedia, and all the previous ones while inaccessible from your code, will still lock your device.

Here is an MCVE

In other words, you need to refactor your code.

You need to keep better track of when a request to get the MediaStream has been made, so to make the less changes in your code, I can propose you to actually store the Promise that gets returned by getUserMedia [instead of / along with] storing the MediaStream.

This way, next calls will just have to then() this Promise, in order to access the same MediaStream.

// outer scope
var stream_request = null;
// [...]
function requestStream() {
  if(!stream_request) {
    stream_request =  navigator.mediaDevices.getUserMedia(options);
  }
  return stream_request
       .then(doSomethingWithTheMediaStream);
}

// and to kill it
function kill_stream() {
  return stream_request.then(stream => {
    stream.getTracks().forEach(t => t.stop());
  }
}

live example

Kaiido
  • 123,334
  • 13
  • 219
  • 285
  • I have tried setting the mediastream first and then adding it to the peerConnections. There is no way that more than one mediaStream is being created. It seems to be the act of adding the mediastream to multiple peers that is the problem that breaks the link between the mediastream reference and the media. – Croftie Nov 23 '18 at 18:45
  • @Croftie as I said in my answer, to be 100% sure of what happens we would need more details about your setup, that you should add as an edit to your original question. But once again, this is the most plausible reason. Whatever you do with this MediaStream afterward should have no incidence on its tracks, and on how they do lock the device. As a debugging step, you can log the mediaStream tracks `id` at getting and when you stop them. If they do differ, you have multiple streams from the same device. – Kaiido Nov 24 '18 at 05:38