forestclown
forestclown

Reputation: 1602

How to properly release audioContext when using volume gain

When using WebRTC, after I have stop the stream, the webcam light will be off, and the "red dot" on chrome tab will be gone. (as seen in image below)

Red dot on tab

vid = document.createElement('VIDEO');
vid.style.width = 100;
vid.style.height = 100;
vid.style.top = 100;
vid.style.left = 0;
vid.style.position = 'absolute';
vid.style.border = '1px solid red';
vid.id = 'xxx';
document.body.insertBefore(vid,document.body.childNodes[0]);
xxx = document.getElementById('xxx');
var xstream = null;

navigator.webkitGetUserMedia({video:true, audio:true},
  function(stream) {
    xstream = stream;    
    xxx.src = window.URL.createObjectURL(stream);
  },
  function(error) {
    console.log('err', error);
  }
);
xxx.play();

So when I do the following:

xstream.stop();

I am good to go, the webcam light is off, and the red dot on tab is gone instantly.

But when I use "audioContext" like below:

vid = document.createElement('VIDEO');
vid.style.width = 100;
vid.style.height = 100;
vid.style.top = 100;
vid.style.left = 0;
vid.style.position = 'absolute';
vid.style.border = '1px solid red';
vid.id = 'xxx';
document.body.insertBefore(vid,document.body.childNodes[0]);
xxx = document.getElementById('xxx');
var xstream = null
    wp = {};

navigator.webkitGetUserMedia({video:true, audio:true},
  function(stream) {
    xstream = stream;

    wp.audio = {
        source: null,
        volume: null,
        enabled: true
    }
    wp.audioContext = new window.AudioContext();
    wp.audio.source = wp.audioContext.createMediaStreamSource(stream);
    wp.audio.volume = wp.audioContext.createGain();
    wp.audio.destination = wp.audioContext.createMediaStreamDestination();
    wp.audio.outputStream = wp.audio.destination.stream;
    wp.audio.source.connect(wp.audio.volume);
    wp.audio.volume.connect(wp.audio.destination);
    stream.addTrack(wp.audio.outputStream.getAudioTracks()[0]);
    stream.removeTrack(stream.getAudioTracks()[0]);

    xxx.src = window.URL.createObjectURL(stream);
  },
  function(error) {
    console.log('err', error);
  }
);
xxx.play();

If I simply do a xstream.stop(), although the webcam light is off, but the red dot is still showing.

But if I do the following:

wp.audio.source.mediaStream.stop();
wp.audio.destination.stream.stop();
wp.audio.outputStream.stop();
wp.audio.source.disconnect();
wp.audio.volume.disconnect();
wp.audio.destination = null;
wp.audio.source = null;
wp.audio.volume = null;
wp.audio.outputStream = null;
wp.audioContext.close();
xstream.stop();
xxx.src = '';

Sometimes the red dot will disappear, most of the time it will not. When it do disappear, it will normally take 20 - 40 seconds, sometimes more.

Not sure what I did wrong here.

Note: You can copy and paste the codes in console to try it out

Upvotes: 1

Views: 1147

Answers (1)

gunblues
gunblues

Reputation: 427

Maybe you can try to stop stream before stream.removeTrack

Upvotes: 1

Related Questions