0.0.4 • Published 10 years ago

mediastream-gain-controller v0.0.4

Weekly downloads
2
License
MIT
Repository
github
Last release
10 years ago

mediastream-gain-controller

What is this?

A tiny module for creating a gain/volume controller for the audio channels in a MediaStream.

It's useful for controlling the volume of your microphone input before it's sent accross a peer connection in a WebRTC call, for example.

Installing

npm install mediastream-gain-controller

This will install mediastream-gain-controller.js that is vanilla js sources and mediastream-gain-controller.min.js which is minified AMD module.

An example

var MicGainController = require('mediastream-gain-controller.min');
var getUserMedia = (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia);
var gainController, microphoneStream;

getUserMedia(function (err, stream) {
    gainController = new MicGainController(stream);
    microphoneStream = gainController.outputStream;
});

function mute() {
    // set gain to 0, effectively muting it
    gainController.setGain(0); // or gainController.off();
}

function unmute() {
    // to unmute call 
    gainController.on(); // equivalent to setGain(1)
}

// set gain to 20%
// gainController.setGain(.2);

If you use SipMl5 as sip client and want to mute/unmute microphone than you can use solution which is described here what means that you will have to implement a lot of tricky things and cases. And of course it is tightly coupled with SipMl stack. But you can use mediastream-gain-controller which allows you not to stick to any sip stack implementation and simply do next:

IMPORTANT

SipMl has a bug :) When you call getUserMedia explicitly or implicitly, browser asks you for microphone access permissions. If you open your web site over https, browser will ask your permissions only once. But if you open you web site over http, it will be asking your permissions every time you make or receive a call. In order to eliminate this annoying behaviour, SipMl provides an option (enable_media_stream_cache = true) which allows to cache getUserMedia results. Unfortunately this feature doesn't work (even in latest version (1.4.217)). To fix that you should apply small patch.

Open tsip_dialog_invite.js and do the following changes:

  • find tmedia_session_mgr.prototype.SetParamSession(o_msession_mgr.e_type, "cache-stream", this.get_stack().network.b_cache_stream),
  • repace it with tmedia_session_mgr.prototype.SetParamSession(o_msession_mgr.e_type, "cache-stream", this.get_stack().media.b_cache_stream),

You can do it even in minified sources. Find .network.b_cache_stream and replace it with .media.b_cache_stream. That's it!

// make sure you enabled simpl getUserMedia stream caching by setting enable_media_stream_cache = true in sipml options
// __o_jsep_stream_audio global var in which sipml stores stream from getUserMedia request
__o_jsep_stream_audio = microphoneStream; // or gainController.outputStream
mute(); // unmute();

With JsSip everything is the same.

// you just need to path microphoneStream to JsSip.UA.call method in options parameter
jssipClient.call(targer, {mediaStream: microphoneStream});
mute(); // unmute();

Methods

You can check for support by checking the support property of the an instance of gainController

  • .setGain(Float) - takes a number between 1 and 0
  • .getGain() - returns current setting
  • .off() - shortcut for turning mic off
  • .on() - shortcut for full gain

License

MIT