WebRTC client

Configuration

Configure your WebRTC SIP client with the following settings.

Client settings

Parameter Value
Websockt Servers api.46elks.com/a1/webrtc/ws
bpi.46elks.com/a1/webrtc/ws
cpi.46elks.com/a1/webrtc/ws
Username Client number without the + sign like 4600100100.
Password The password for the number it is the value "secret" on the number object in the API.
URI Yhe server and number in combination like 4600100100@voip.46elks.com

Sample code


<script src="/static/js/libs/sip-0.11.2.min.js"></script>
<script>

  // Initiate client
  var outinnumber = "+46766861004";
  var session = {"status":0};
  var userAgent = new SIP.UA({ 
    transportOptions: {
      wsServers: [
        'wss://api.46elks.com/a1/webrtc/ws',
        'wss://bpi.46elks.com/a1/webrtc/ws',
        'wss://cpi.46elks.com/a1/webrtc/ws']
      },
      uri: '4600100100@voip.46elks.com',
      authorizationUser: '4600100100',
      password: 'AAAAAAAAAAAAAAAAAAAAAAAAAA',
      registerExpires: 60,
      sessionDescriptionHandlerFactoryOptions: {
        constraints: {
          audio: true,
          video: false
        }
      }
    }
  );

  // Initiate ring sound
  var ringAudio = new Audio('/static/sound/ring.mp3');
  ringAudio.loop = true;

  // Handle new call
  userAgent.on('invite', 
    function (incomingSession) {
      // If call is ongloing terminate new call.
      if (session.status == 12) {
        incomingSession.terminate()
        return;
      }

      // Set global call to incomming call.
      session = incomingSession;

      // Add sound to browser when call is connected.
      session.on('accepted', addsound);

      // If the call is comming from self (outgoing call)
      // make automatic pickup.
      if(session.remoteIdentity.uri.user == outinnumber){
        session.accept();
      }
      // In other case play ringtone.
      else {
        ringAudio.play();
      }
    }
  );

  // Add sound from the remote caller to the browser.
  function addsound(){
    var domElement = document.getElementById('audio');
    var pc = session.sessionDescriptionHandler.peerConnection;
    var remoteStream = new MediaStream();
    pc.getReceivers().forEach(
      function(receiver) {
        var track = receiver.track;
        if (track) {
          remoteStream.addTrack(track);
        }
      }
    );
    domElement.srcObject = remoteStream;
    domElement.play();
  }

  // Initate outgoing all with a request to the API for call.
  function startCall(to) {
    var domElement = document.getElementById('audio');
    domElement.play();
    $.post("/yourcallendpoint",
      {
        voice_start: '{"connect":"'+to+'"}',
        to: "+4600100100",
        from: outinnumber,
      },
      function(data, status) {}
    );
  }
</script>
<audio id="audio">
<button onclock="session.accept();">Pickup</button>
<button onclock="session.bye();">Hangup</button>
<button onclock="startCall('+4634090510')">Call</button>

Calls from client

If you initate calls direclty from the client, instead of initating them from the API, the call is handled buy the voice_start paramter on the number object.