Skip to content

Instantly share code, notes, and snippets.

@echohes
Created August 3, 2018 07:20
Show Gist options
  • Save echohes/a15fcef59e78271d7a3acb0df480b6b6 to your computer and use it in GitHub Desktop.
Save echohes/a15fcef59e78271d7a3acb0df480b6b6 to your computer and use it in GitHub Desktop.
Jssip (jssip-3.2.11.min.js) WebPhone (Video Calling Example)
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Sip demo</title>
</head>
<body>
<head><title>WebRT</title>
<style>
video { height: 240px; width: 320px; border: 3px solid grey; }
</style>
</head>
<video id="selfView" autoplay muted=true></video>
<video id="remoteView" autoplay></video>
</body>
<script src="jssip-3.2.11.min.js"> </script>
<script>
var socket = new JsSIP.WebSocketInterface('wss://${{SERVER}}/ws');
socket.via_transport = "tcp";
//Create HTML Audio Object
var remoteAudio = new window.Audio()
remoteAudio.autoplay = true;
const mediaSource = new MediaSource();
var selfView = document.getElementById('selfView');
var remoteView = document.getElementById('remoteView');
var user = "${{USERNAME}}";
var pass = "${{PASSWORD}}";
var userAgent = JsSIP.version;
console.log('sip:%s@${{SERVER}}', user);
var configuration = {
'uri': 'sip:'+ user + '@${{SERVER}}',
'password': pass, // FILL PASSWORD HERE,
'sockets': [ socket ],
'register_expires': 180,
'session_timers': false,
'user_agent' : 'JsSip-' + userAgent
};
var phone;
if(user && pass){
JsSIP.debug.enable('JsSIP:*');
phone = new JsSIP.UA(configuration);
phone.on('registrationFailed', function(ev){
alert('Registering on SIP server failed with error: ' + ev.cause);
configuration.uri = null;
configuration.password = null;
});
phone.on('newRTCSession',function(ev){
var newSession = ev.session;
if(session){ // hangup any existing call
session.terminate();
}
session = newSession;
var completeSession = function(){
session = null;
};
if(session.direction === 'outgoing'){
console.log('stream outgoing -------->');
session.on('connecting', function() {
console.log('CONNECT');
});
session.on('peerconnection', function(e) {
console.log('1accepted');
});
session.on('ended', completeSession);
session.on('failed', completeSession);
session.on('accepted',function(e) {
console.log('accepted')
});
session.on('confirmed',function(e){
console.log('CONFIRM STREAM');
});
};
if(session.direction === 'incoming'){
console.log('stream incoming -------->');
session.on('connecting', function() {
console.log('CONNECT');
});
session.on('peerconnection', function(e) {
console.log('1accepted');
add_stream();
});
session.on('ended', completeSession);
session.on('failed', completeSession);
session.on('accepted',function(e) {
console.log('accepted')
});
session.on('confirmed',function(e){
console.log('CONFIRM STREAM');
});
var options = {
'mediaConstraints' : { 'audio': true, 'video': true },
'pcConfig': {
'rtcpMuxPolicy': 'require',
'iceServers': [
]
},
};
console.log('Incoming Call');
session.answer(options);
}
});
phone.start();
}
var session;
function callAsterisk(numTels) {
var options = {
'mediaConstraints' : { 'audio': true, 'video': true },
'pcConfig': {
'rtcpMuxPolicy': 'require',
'iceServers': [
]
},
};
var numTel = numTels.toString();
var num = '200';
console.log(numTel);
phone.call(numTel, options)
add_stream();
};
function add_stream(){
session.connection.addEventListener('addstream',function(e) {
remoteAudio.srcObject = (e.stream);
remoteView.srcObject = (e.stream);
selfView.srcObject = (session.connection.getLocalStreams()[0]);
})
}
</script>
<p>
<a href="javascript:callAsterisk(123)">Test</a>
<a href="javascript:callAsterisk(777)">Echo</a>
<a href="javascript:callAsterisk(501)">Call to 501</a>
@tamtc84
Copy link

tamtc84 commented Nov 5, 2019

Hi Mr echohes,
I'm beginner with jssip. I run your code. When i make call, after allow mic and camera, session stop. I receive:

JsSIP:WebSocketInterface send() +2ms
jssip-3.2.11.min.js:9 JsSIP:RTCSession receiveInviteResponse() +74ms
jssip-3.2.11.min.js:9 JsSIP:RTCSession session failed +0ms
jssip-3.2.11.min.js:9 JsSIP:RTCSession close() +1ms
jssip-3.2.11.min.js:9 JsSIP:RTCSession close() | closing local MediaStream +3ms
jssip-3.2.11.min.js:9 JsSIP:RTCSession emit "failed" +2ms
jssip-3.2.11.min.js:9 JsSIP:InviteClientTransaction Timer D expired for transaction z9hG4bK5776136 +39ms

Please help me.

@echohes
Copy link
Author

echohes commented Nov 5, 2019

Hi Mr echohes,
I'm beginner with jssip. I run your code. When i make call, after allow mic and camera, session stop. I receive:

JsSIP:WebSocketInterface send() +2ms
jssip-3.2.11.min.js:9 JsSIP:RTCSession receiveInviteResponse() +74ms
jssip-3.2.11.min.js:9 JsSIP:RTCSession session failed +0ms
jssip-3.2.11.min.js:9 JsSIP:RTCSession close() +1ms
jssip-3.2.11.min.js:9 JsSIP:RTCSession close() | closing local MediaStream +3ms
jssip-3.2.11.min.js:9 JsSIP:RTCSession emit "failed" +2ms
jssip-3.2.11.min.js:9 JsSIP:InviteClientTransaction Timer D expired for transaction z9hG4bK5776136 +39ms

Please help me.

Hi! Which browser do you use?
Try turning on debug, in console (or in code):

JsSIP.debug.enable('JsSIP:*');

@tamtc84
Copy link

tamtc84 commented Nov 6, 2019

Thanks for reply,

  • With Chrome 78, after callee confirm answer, no sound avaiable.
  • With firefox, after callee confirm answer, no sound to talk and console is:
JsSIP:RTCSession emit "confirmed" +0ms  jssip-3.2.11.min.js:9:228932
CONFIRM STREAM  jssip_test.html:88:13
RTCPeerConnection.getLocalStreams/getRemoteStreams are deprecated. Use RTCPeerConnection.getSenders/getReceivers instead.  jssip_test.html:150:38
JsSIP:WebSocketInterface received WebSocket message +5s  jssip-3.2.11.min.js:9:228932
JsSIP:Transport received text message:

@echohes
Copy link
Author

echohes commented Nov 8, 2019

Apparently, browsers again changed the method of working with media.

RTCPeerConnection.getLocalStreams/getRemoteStreams are deprecated. Use RTCPeerConnection.getSenders/getReceivers instead

@echohes
Copy link
Author

echohes commented Nov 8, 2019

Try replacing getLocalStreams with getSenders.
Over the weekend I will update the code as I test the changes

@tamtc84
Copy link

tamtc84 commented Nov 8, 2019

thank you.

@haisamMH
Copy link

I want to switch video on/off while ongoing call, how is it possible?

@echohes
Copy link
Author

echohes commented May 4, 2020

I want to switch video on/off while ongoing call, how is it possible?

Hi, im used this method mute/unmute from jssip api
or difficult way to do reinvite/update where in the SDP you need to exclude the video
https://jssip.net/documentation/3.4.x/api/session/#event_reinvite

@sailfromthemoon
Copy link

Hi, why can't I see remote video?

@echohes
Copy link
Author

echohes commented Jun 29, 2020

Hi, why can't I see remote video?

Hi!
You need to make sure that you get remote media in the browser settings
chrome://webrtc-internals/
and be sure to watch debug

@sailfromthemoon
Copy link

Thank you for reply!
I commented this line
//remoteAudio.srcObject = (e.stream);
and now it works!

@athiraaravindan
Copy link

please give an advice how to implement screen sharing on jssip

@echohes
Copy link
Author

echohes commented Feb 12, 2021

please give an advice how to implement screen sharing on jssip

In mediaConstraints need add for video:
mediaSource : 'screen'

@chung-lt
Copy link

chung-lt commented Mar 9, 2021

hello every body. my problem is remote video not showing on one end.
eg: Bob call to Alice
-> when Alice picked up the phone, Bob side can see both Bob video and Alice video
but Alice side can not see Bob video, only can see Alice video
can someone talk to me where I am wrong, and how to fix it
thankyou so much

@echohes
Copy link
Author

echohes commented Mar 10, 2021

hello every body. my problem is remote video not showing on one end.
eg: Bob call to Alice
-> when Alice picked up the phone, Bob side can see both Bob video and Alice video
but Alice side can not see Bob video, only can see Alice video
can someone talk to me where I am wrong, and how to fix it
thankyou so much

Hello! in chrome, you can debug your active call: chrome://webrtc-internals.
It is necessary to check on Alice's side whether she receives a media stream from Bob.
You can check which IP addresses come to the SDP, from Alice to Bob. (It is possible that the stream from Bob goes to 127.0.0.1)
What voip-server is used for jssip? (Freeswitch, Kamailio/Opensips(rtpengine), Asterisk).

@chung-lt
Copy link

thank you for the reply, my problem has been solved
currently i want to make an initial call with only audio
'mediaConstraints': { 'audio': true, 'video': false },

then I want to use renegotiate method to send re-invite and add sdp m = video to turn on video call

my function:
async function on_video(){
var options = {
'rtcOfferConstraints':{
'iceRestart' : true,
'offerToReceiveAudio' : true,
'offerToReceiveVideo' : true
},
}
console.log("on video");
current_session.renegotiate(options);
}

however I am not successful yet, can you give me advice

@echohes
Copy link
Author

echohes commented Apr 19, 2021

thank you for the reply, my problem has been solved
currently i want to make an initial call with only audio
'mediaConstraints': { 'audio': true, 'video': false },

then I want to use renegotiate method to send re-invite and add sdp m = video to turn on video call

my function:
async function on_video(){
var options = {
'rtcOfferConstraints':{
'iceRestart' : true,
'offerToReceiveAudio' : true,
'offerToReceiveVideo' : true
},
}
console.log("on video");
current_session.renegotiate(options);
}

however I am not successful yet, can you give me advice

You can use method reinvite
or the MUTE mode for video call

@Cruzzhere
Copy link

Hi ,
I'm beginner with jssip. I run your code. When i make call, after allow mic and camera,I receive:
JsSIP:RTCSession receiveInviteResponse() +1ms
JsSIP.min.js:27083 JsSIP:Dialog new UAC dialog created with status CONFIRMED +0ms
JsSIP.min.js:27083 JsSIP:RTCSession emit "sdp" +1ms

Uncaught TypeError: Failed to set the 'srcObject' property on 'HTMLMediaElement': The provided value is not of type '(MediaSourceHandle or MediaStream)'.
at RTCPeerConnection. (oldEg.html:182:39)
(anonymous) @ oldEg.html:182

JsSIP.min.js:27083 JsSIP:RTCSession session accepted +2ms
JsSIP.min.js:27083 JsSIP:RTCSession emit "accepted" +0ms
oldEg.html:123 accepted

Help me out

@echohes
Copy link
Author

echohes commented Jan 23, 2023

Hi @Cruzzhere, what browser are you using?

@Cruzzhere
Copy link

@echohes Google Chrome Version 108.0.5359.124

@echohes
Copy link
Author

echohes commented Jan 26, 2023

@Cruzzhere I can't check right now, I will have to run a test server. Try replacing the method with an object, in add_stream func:

const remoteAudio = document.createElement('audio');
remoteAudio.src = window.URL.createObjectURL(e.stream);
remoteAudio.play();

@shwetasinghx
Copy link

hi new with jssip while using ur code im getting document is not defined ,window is not defined how do i solve it please help

@AndreyVolkov88
Copy link

How can I turn on the webcam on my laptop to send video to subscriber 2?

@echohes
Copy link
Author

echohes commented Apr 5, 2023

document is not defined

Hi! Is your code running in the browser?

@echohes
Copy link
Author

echohes commented Apr 5, 2023

How can I turn on the webcam on my laptop to send video to subscriber 2?

I understand correctly, you have 2 active calls, and only one need activate video?

@dr-plugin
Copy link

Hello
I am using this code and it works on android and desktop
But the sound does not play in the ios and chrome browser

In Safari it doesn't work at all !!!!!

@echohes
Copy link
Author

echohes commented May 11, 2023

Hello I am using this code and it works on android and desktop But the sound does not play in the ios and chrome browser

In Safari it doesn't work at all !!!!!

Hi!
As far as I remember, the mechanism of working with i/o devices is implemented differently in browsers. It is necessary to read the documentation for Safari. Because I did a different implementation between google chrome and firefox.

@dr-plugin
Copy link

Thanks

@m-yunus
Copy link

m-yunus commented Nov 8, 2024

Hello! I’m new to JSSIP and am exploring ways to improve the handling of outgoing calls. When I make an outgoing call, there’s no response if the callee is busy or their phone is switched off. Is there a way to enable early media or similar functionality in JSSIP to receive feedback in these cases? Any guidance or examples would be much appreciated. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment