How to get localStream when receiving call in SIP.js (to mute microphone)

2.5k Views Asked by At

When I want to mute microphone I use mediastream which I get from

session.sessionDescriptionHandler.on('userMedia', 
onUserMediaObtained.bind(this))

function onUserMediaObtained(stream) {
    localMediaStream = stream
}

Session value is from agent.invite() method.

But when I get incoming call, in onInvite event handler:

agent.on('invite', onInvite(dispatch, store))

const onInvite = (dispatch, store) => session => {
  if (session.sessionDescriptionHandler) {}
  else { //always undefined here}
 }

I try again attach event in onaccepted event handler

session.on('accepted', onAccepted(dispatch))

There is sessionDescriptionHandler object initialized, but I think it's too late, onUserMediaObtained isn't fired.

3

There are 3 best solutions below

0
On
let pc = this.session.sessionDescriptionHandler.peerConnection;
pc.getLocalStreams().forEach(function (stream) {
    stream.getAudioTracks().forEach(function (track) {
        try {
            track.enabled = !track.enabled;
        } catch (e) {
            toastr.error('Error occured in executing this command.');
            console.log(e);
        }
    });
});
1
On

You can use the session object to mute/unmute the microphone. It seems that these two functions have been removed from the Session API page, but I'm pretty sure it still works.

In this the 0.7.0 API page you have it specified https://sipjs.com/api/0.7.0/session/#muteoptions

0
On

here I just did this with sipjs 11

`const pc = session.sessionDescriptionHandler.peerConnection;
        let togglemute = `true/false`;
        // this.emit
        if (pc.getSenders) {
            pc.getSenders().forEach(function (sender) {
                if (sender.track) {
                    sender.track.enabled = !togglemute;

                }
            });
        } else {
            pc.getReceivers().forEach(function (receiver) {
                if (receiver.track) {
                    receiver.track.enabled = !togglemute;
                }
            });
        }`