Guides

Reuse MediaStreams

In order to provide a more streamlined user experience, applications built with SIP.js may wish to minimize the number of times the user is prompted to allow access to their microphone/camera. This is especially useful when the user makes multiple calls, since only Chrome and Opera on secure (i.e. https://) pages will remember media allowances.

When making or receiving a call, SIP.js allows applications to specify the desired MediaStream as an option to UA.invite and Session.accept, respectively. SIP.js applications can get a reference to a MediaStream using SIP.WebRTC.getUserMedia.

Setup

Note: the following code blocks assume you already have a SIP.UA named ua. Please see the User Agent Construction guide for instructions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  var mediaStream;

  var mediaConstraints = {
    audio: true,
    video: true
  };

  function getUserMediaSuccess (stream) {
    console.log('getUserMedia succeeded', stream)
    mediaStream = stream;
  }

  function getUserMediaFailure (e) {
    console.error('getUserMedia failed:', e);
  }

  SIP.WebRTC.getUserMedia(mediaConstraints, getUserMediaSuccess, getUserMediaFailure);

After getUserMediaSuccess has been called, the stream can be used to make or receive calls.

Making a call

1
2
3
4
5
6
7
  var target = 'someone@domain.tld';
  var options = {
    media: {
      stream: mediaStream
    }
  };
  ua.invite(target, options);

Receiving a call

1
2
3
4
5
6
7
8
  ua.on('invite', function (session) {
    var options = {
      media: {
        stream: mediaStream
      }
    };
    session.accept(options);
  });

Live Example