sir,can you please take a look at code? I dont need incoming call. Just outgoing call to an ivr system.
let janus = null;
let sipcall = null;
let opaqueId = “sipcall-” + Janus.randomString(12);
let localStream = null;
let remoteStream = null;
let localTracks = {}, localVideos = 0;
let remoteTracks = {}, remoteVideos = 0;
let doAudio = true, doVideo = true;
let isMuted = false;
let isVideoMuted = false;
let helpersCreated = false;
// Array to store helper handles
let helpers = ;
document.addEventListener(‘DOMContentLoaded’, function () {
initializeJanus();
document.getElementById(‘callButton’).addEventListener(‘click’, toggleCall);
document.getElementById(‘muteButton’).addEventListener(‘click’, toggleMute);
document.getElementById(‘videoButton’).addEventListener(‘click’, toggleVideo);
document.querySelectorAll(‘.dtmf’).forEach(button => {
button.addEventListener(‘click’, function () {
sendDTMF(this.getAttribute(‘data-digit’));
});
});
});
function initializeJanus() {
Janus.init({
debug: “all”,
callback: function () {
if (!Janus.isWebrtcSupported()) {
alert("No WebRTC support… ");
return;
}
janus = new Janus({
server: ‘ws://1.1.1.1:8188/’,
success: function () {
attachMainHandle();
},
error: function (error) {
console.error(error);
alert(error);
},
destroyed: function () {
window.location.reload();
}
});
}
});
}
function attachMainHandle() {
janus.attach({
plugin: “janus.plugin.sip”,
opaqueId: opaqueId,
success: function (pluginHandle) {
sipcall = pluginHandle;
console.log(“Master session ID:”, sipcall.getId());
console.log(“Plugin attached! (” + sipcall.getPlugin() + “, id=” + sipcall.getId() + “)”);
registerUsername(sipcall);
},
error: function (error) {
console.error(" – Error attaching plugin…", error);
alert(“Error attaching plugin… " + error);
},
consentDialog: function (on) {
console.log(“Consent dialog should be " + (on ? “on” : “off”) + " now”);
},
mediaState: function (medium, on) {
console.log(“Janus " + (on ? “started” : “stopped”) + " receiving our " + medium);
},
webrtcState: function (on) {
console.log(“Janus says our WebRTC PeerConnection is " + (on ? “up” : “down”) + " now”);
},
onmessage: handleMessage,
onlocaltrack: handleLocalTrack,
onremotetrack: handleRemoteTrack,
oncleanup: function () {
console.log(” ::: Got a cleanup notification :::”);
localStream = null;
remoteStream = null;
}
});
}
function createHelpers(count, masterId) {
console.log(“Creating helpers with master ID:”, masterId);
for (let i = 0; i < 10; i++) { // Create only 10 helpers{
janus.attach({
plugin: “janus.plugin.sip”,
opaqueId: opaqueId + “-helper-” + i,
success: function (pluginHandle) {
helpers.push(pluginHandle);
console.log(“Helper plugin attached! (id=” + pluginHandle.getId() + “)”);
registerUsername(pluginHandle, true, masterId);
},
error: function (error) {
console.error(" – Error attaching helper plugin…“, error);
},
onmessage: handleMessage,
onlocaltrack: handleLocalTrack,
onremotetrack: handleRemoteTrack,
oncleanup: function () {
console.log(” ::: Got a cleanup notification for helper :::");
}
});
}
}
function registerUsername(handle, isHelper = false, masterId = null) {
let formData = JSON.parse(localStorage.getItem(‘formData’));
if (formData && formData.name && formData.phone) {
let register = {
request: “register”,
type: isHelper ? “helper” : “guest”,
username: “sip:” + formData.name + (isHelper ? “-helper” : “”) + “@abc…com”,
display_name: formData.name + " " + formData.phone + (isHelper ? " (Helper)" : “”),
secret: “cisco123”,
};
if (isHelper && masterId) {
register["master_id"] = masterId;
}
handle.send({ message: register });
} else {
alert("User data not found. Please go back and submit the form.");
}
}
function handleMessage(msg, jsep) {
console.log(" ::: Got a message :::", msg);
let result = msg[“result”];
if (result) {
if (result[“event”]) {
let event = result[“event”];
if (event === ‘registered’) {
console.log("Successfully registered as " + result[“username”] + “!”);
document.getElementById(‘callButton’).disabled = false;
} else if (event === ‘calling’) {
console.log(“Calling…”);
document.getElementById(‘callButton’).textContent = ‘Calling…’;
document.getElementById(‘callButton’).disabled = true;
} else if (event === ‘incomingcall’) {
console.log("Incoming call from " + result[“username”] + “!”);
} else if (event === ‘hangup’) {
console.log(“Call hung up (” + result[“code”] + " " + result[“reason”] + “)!”);
document.getElementById(‘callButton’).textContent = ‘Call’;
document.getElementById(‘callButton’).disabled = false;
}
}
}
if (jsep) {
console.log("Handling SDP as well...", jsep);
sipcall.handleRemoteJsep({ jsep: jsep });
}
}
function handleLocalTrack(track, on) {
console.log("Local track " + (on ? “added” : “removed”) + “:”, track);
let localVideo = document.getElementById(‘localVideo’);
if (on) {
if (localStream === null) {
localStream = new MediaStream();
}
localStream.addTrack(track);
Janus.attachMediaStream(localVideo, localStream);
} else {
localStream.removeTrack(track);
if (localStream.getTracks().length === 0) {
Janus.attachMediaStream(localVideo, null);
}
}
}
function handleRemoteTrack(track, mid, on) {
console.log(“Remote track (mid=” + mid + ") " + (on ? “added” : “removed”) + “:”, track);
let remoteVideo = document.getElementById(‘remoteVideo’);
if (on) {
if (remoteStream === null) {
remoteStream = new MediaStream();
}
remoteStream.addTrack(track);
Janus.attachMediaStream(remoteVideo, remoteStream);
} else {
remoteStream.removeTrack(track);
if (remoteStream.getTracks().length === 0) {
Janus.attachMediaStream(remoteVideo, null);
}
}
}
function toggleCall() {
if (sipcall.webrtcStuff.pc) {
let hangup = { request: “hangup” };
sipcall.send({ message: hangup });
sipcall.hangup();
document.getElementById(‘callButton’).textContent = ‘Call’;
} else {
let sipUri = ‘sip:0000@abc…com’;
navigator.mediaDevices.getUserMedia({ audio: true, video: true })
.then(stream => {
localStream = stream;
// Create offer after local stream is available
sipcall.createOffer({
streams: [localStream],
success: function(jsep) {
let body = { request: “call”, uri: sipUri };
sipcall.send({ message: body, jsep: jsep });
},
error: function(error) {
console.error(“WebRTC error…”, error);
}
});
})
.catch(error => {
console.error(“Error accessing user media:”, error);
});
}
}
function toggleMute() {
let muted = sipcall.isAudioMuted();
if(muted) {
sipcall.unmuteAudio();
document.getElementById(‘muteButton’).textContent = ‘Mute’;
} else {
sipcall.muteAudio();
document.getElementById(‘muteButton’).textContent = ‘Unmute’;
}
}
function toggleVideo() {
let videoMuted = sipcall.isVideoMuted();
if(videoMuted) {
sipcall.unmuteVideo();
document.getElementById(‘videoButton’).textContent = ‘Video Off’;
} else {
sipcall.muteVideo();
document.getElementById(‘videoButton’).textContent = ‘Video On’;
}
}
function sendDTMF(digit) {
if(sipcall.dtmf) {
sipcall.dtmf({dtmf: { tones: digit }});
}
}