@ -36,20 +36,6 @@ const UASimplesIP = (function () {
}
} ) ;
function extrairNome ( displayName ) {
// Expressão regular para extrair o nome entre aspas
const regex = /"([^"]+)"/ ;
// Executa a expressão regular na string de displayName
const match = regex . exec ( displayName ) ;
// Se houver correspondência, retorna o primeiro grupo capturado (o nome)
if ( match && match . length > 1 ) {
return match [ 1 ] ;
} else {
// Se não houver correspondência, retorna null ou uma string vazia, dependendo do seu caso
return null ;
}
}
class EventEmitter {
constructor ( ) {
this . events = { } ;
@ -97,6 +83,8 @@ const UASimplesIP = (function () {
}
const _Autenticacao = new Autenticacao ( ) ;
var localAudio = new window . Audio ( ) ;
localAudio . autoplay = true ;
const incomingCallAudio = new window . Audio (
"https://cdn.pixabay.com/download/audio/2021/08/04/audio_bb630cc098.mp3?filename=short-success-sound-glockenspiel-treasure-video-game-6346.mp3"
@ -142,9 +130,7 @@ const UASimplesIP = (function () {
eventoSip . on ( "endCall" , ( ) => {
if ( session && session . isEstablished ( ) ) {
eventoSip . emit ( "confirmedEnded" ) ;
console . log (
":::::::::::::::::::::::evento emitido::::::::::::::::::::::"
) ;
console . log ( ) ;
session . terminate ( ) ;
}
} ) ;
@ -152,16 +138,12 @@ const UASimplesIP = (function () {
eventoSip . on ( "evento" , ( payload ) => {
console . log ( payload ) ;
} ) ;
eventoSip . on ( "mute" , function ( ) {
console . log ( "MUTADO:::::::::::::::" ) ;
session . mute ( ) ;
} ) ;
eventoSip . on ( "unmute" , function ( ) {
if ( session && session . isEstablished ( ) && session . isMuted ( ) ) {
session . unmute ( ) ;
}
session . unmute ( ) ;
} ) ;
session . on ( "failed" , ( ) => { } ) ;
@ -170,11 +152,26 @@ const UASimplesIP = (function () {
} ) ;
session . on ( "confirmed" , function ( confirmed ) {
// Verifica se session.connection está definido
if (
session . connection &&
session . connection . getRemoteStreams ( ) . length > 0
) {
const remoteStreams = session . connection . getRemoteStreams ( ) [ 0 ] ;
remoteAudio . srcObject = remoteStreams ;
}
// Verifica se session.connection está definido e se existem streams locais
if (
session . connection &&
session . connection . getLocalStreams ( ) . length > 0
) {
const localStreams = session . connection . getLocalStreams ( ) [ 0 ] ;
localAudio . srcObject = localStreams ;
}
eventoSip . emit ( "incall" ) ;
const remoteStreams = session . connection . getRemoteStreams ( ) [ 0 ] ;
remoteAudio . srcObject = remoteStreams ;
} ) ;
session . on ( "icecandidate" , function ( event ) {
if (
event . candidate . type === "srflx" &&
@ -184,16 +181,19 @@ const UASimplesIP = (function () {
event . ready ( ) ;
}
} ) ;
session . on ( "addstream" , function ( e ) {
remoteAudio . src = window . URL . createObjectURL ( e . stream ) ;
// Verifica se session.connection está definido
if ( session . connection ) {
remoteAudio . src = window . URL . createObjectURL ( e . stream ) ;
}
} ) ;
//RECEBENDO UMA CHAMADA
if ( session . direction === "incoming" ) {
incomingCallAudio . play ( ) ;
console . log ( "::::::::::::::::::::::chamada recebida" ) ;
isIncomingCall = true ;
eventoSip . emit ( "incomingcall" , extrairNome ( session . remote _identity ) ) ;
eventoSip . emit ( "incomingcall" , session . remote _identity . uri . user ) ;
eventoSip . on ( "rejected" , ( ) => {
if ( session && session . isInProgress ( ) ) {
@ -215,15 +215,8 @@ const UASimplesIP = (function () {
}
//REALIZANDO UMA CHAMADA
if ( session . direction === "outgoing" ) {
console . log ( "::::::::::::::::::::::chamada realizada" ) ;
eventoSip . emit ( "outgoingcall" ) ;
eventoSip . emit ( "outgoingcall" , session . remote _identity . uri . user ) ;
}
// //CHAMADA EM ANDAMENTO
// if (session.direction === "progress") {
// incomingCallAudio.pause();
// eventoSip.emit("incall");
// console.log("::::::::::::::::::::::chamada em progresso");
// }
} ) ;
phone . start ( ) ;
}