RTC session or connection is not ready

Hello. I’m trying to develop web phone using cyber mega phone. When reeving the incoming call, following issue occurred. how fix it?

RTC session or connection is not ready. cyber_mega_phone.js:122:21
    handleNewSession file:///D:/DMS/Daily task/New folder (7)/cyber-mega-phone/cyber_mega_phone_2k-master/cyber_mega_phone.js:122
    connect file:///D:/DMS/Daily task/New folder (7)/cyber-mega-phone/cyber_mega_phone_2k-master/cyber_mega_phone.js:87
    emit file:///D:/DMS/Daily task/New folder (7)/cyber-mega-phone/cyber_mega_phone_2k-master/lib/jssip-3.0.13.js:21730
    newRTCSession file:///D:/DMS/Daily task/New folder (7)/cyber-mega-phone/cyber_mega_phone_2k-master/lib/jssip-3.0.13.js:19851
    newRTCSession file:///D:/DMS/Daily task/New folder (7)/cyber-mega-phone/cyber_mega_phone_2k-master/lib/jssip-3.0.13.js:16450
    init_incoming file:///D:/DMS/Daily task/New folder (7)/cyber-mega-phone/cyber_mega_phone_2k-master/lib/jssip-3.0.13.js:14009
    receiveRequest file:///D:/DMS/Daily task/New folder (7)/cyber-mega-phone/cyber_mega_phone_2k-master/lib/jssip-3.0.13.js:19958
    onTransportData file:///D:/DMS/Daily task/New folder (7)/cyber-mega-phone/cyber_mega_phone_2k-master/lib/jssip-3.0.13.js:20585
    onData file:///D:/DMS/Daily task/New folder (7)/cyber-mega-phone/cyber_mega_phone_2k-master/lib/jssip-3.0.13.js:19317
    onMessage file:///D:/DMS/Daily task/New folder (7)/cyber-mega-phone/cyber_mega_phone_2k-master/lib/jssip-3.0.13.js:21415
    (Async: EventHandlerNonNull)
    connect file:///D:/DMS/Daily task/New folder (7)/cyber-mega-phone/cyber_mega_phone_2k-master/lib/jssip-3.0.13.js:21342
    connect file:///D:/DMS/Daily task/New folder (7)/cyber-mega-phone/cyber_mega_phone_2k-master/lib/jssip-3.0.13.js:19205
    start file:///D:/DMS/Daily task/New folder (7)/cyber-mega-phone/cyber_mega_phone_2k-master/lib/jssip-3.0.13.js:19546
    connect file:///D:/DMS/Daily task/New folder (7)/cyber-mega-phone/cyber_mega_phone_2k-master/cyber_mega_phone.js:103
    onload file:///D:/DMS/Daily task/New folder (7)/cyber-mega-phone/cyber_mega_phone_2k-master/index.html:254
    (Async: EventListener.handleEvent)
    onload file:///D:/DMS/Daily task/New folder (7)/cyber-mega-phone/cyber_mega_phone_2k-master/index.html:184
    (Async: EventHandlerNonNull)
    <anonymous> file:///D:/DMS/Daily task/New folder (7)/cyber-mega-phone/cyber_mega_phone_2k-master/index.html:24

this is my cyber_mega_phone.js code

'use strict';

// Turn on jssip debugging by un-commenting the below:
//JsSIP.debug.enable('JsSIP:*');

let isFirefox = typeof InstallTrigger !== 'undefined';
let isChrome = !!window.chrome && !!window.chrome.webstore;

function CyberMegaPhone(id, name, password, host, register, audio = true, video = true) {
    EasyEvent.call(this);
    this.id = id;
    this.name = name;
    this.password = password;
    this.host = host;
    this.register = register;
    this.audio = audio;
    this.video = video;

    navigator.mediaDevices.enumerateDevices()
    .then(devices => {
        const mics = devices.filter(device => device.kind === "audioinput");
        const cams = devices.filter(device => device.kind === "videoinput");

        if (mics.length === 0) {
            console.log("Microphone not available, disabling audio.");
            this.audio = false;
        }
        if (cams.length === 0) {
            console.log("Camera not available, disabling video.");
            this.video = false;
        }
    });

    this._locals = new Streams();
    this._locals.bubble("streamAdded", this);
    this._locals.bubble("streamRemoved", this);

    this._remotes = new Streams();
    this._remotes.bubble("streamAdded", this);
    this._remotes.bubble("streamRemoved", this);

    this._rtc = null;
}

CyberMegaPhone.prototype = Object.create(EasyEvent.prototype);
CyberMegaPhone.prototype.constructor = CyberMegaPhone;

function isUnifiedPlanDefault() {
    if (!('currentDirection' in RTCRtpTransceiver.prototype)) {
        return false;
    }

    const tempPc = new RTCPeerConnection();
    let canAddTransceiver = false;
    try {
        tempPc.addTransceiver('audio');
        canAddTransceiver = true;
    } catch (e) {}

    tempPc.close();
    return canAddTransceiver;
}

CyberMegaPhone.prototype.connect = function () {
    if (this._ua) {
        this._ua.start(); // Reconnect if already initialized
        return;
    }

    let socket = new JsSIP.WebSocketInterface('wss://' + this.host + ':8089/ws');
    let uri = 'sip:' + this.id + '@' + this.host;

    let config = {
        sockets: [socket],
        uri: uri,
        contact_uri: uri,
        username: this.name || this.id,
        password: this.password,
        register: this.register,
        register_expires: 300
    };

    this._unified = isUnifiedPlanDefault();
    this._ua = new JsSIP.UA(config);

    this._ua.on('newRTCSession', (data) => {
        this.handleNewSession(data);
    });

    let that = this;
    function bubble(obj, name) {
        obj.on(name, function (data) {
            that.raise(name, data);
        });
    }

    bubble(this._ua, 'connected');
    bubble(this._ua, 'disconnected');
    bubble(this._ua, 'registered');
    bubble(this._ua, 'unregistered');
    bubble(this._ua, 'registrationFailed');

    this._ua.start();
};

CyberMegaPhone.prototype.handleNewSession = function (data) {
    if (data.originator === "remote") {
        this._rtc = data.session;
        let incomingSDP = data.request.body;

        document.getElementById("caller-id").innerText = "Call from: " + data.request.ruri.user;
        showIncomingCallModal();
        this.raise('incoming', [data.request.ruri.toAor()]); // Wrap data in an array
		

        if (this._rtc && this._rtc.connection) {
            this._rtc.connection.setRemoteDescription(new RTCSessionDescription({type: 'offer', sdp: incomingSDP}))
            .then(() => this._rtc.connection.createAnswer())
            .then(answer => this._rtc.connection.setLocalDescription(answer))
            .catch(error => console.error("Error setting up the SDP:", error));
        } else {
            console.error("RTC session or connection is not ready.");
        }
    }
};



CyberMegaPhone.prototype.disconnect = function () {
    this._locals.removeAll();
    this._remotes.removeAll();
    if (this._ua) {
        this._ua.stop();
    }
};

CyberMegaPhone.prototype.answer = function () {
    if (!this._rtc || !this._rtc.answer) {
        console.error("RTC session is not ready or answer method is not available.");
        return;
    }

    let options = {
        'mediaConstraints': { 'audio': this.audio, 'video': this.video }
    };

    try {
        this._rtc.answer(options);
        console.log("Call answered successfully.");
    } catch (error) {
        console.error("Error answering call:", error);
    }
};

CyberMegaPhone.prototype.call = function (exten) {
    if (!this._ua || !exten) {
        console.error("UA session is not ready or extension is undefined.");
        return;
    }

    let options = {
        'mediaConstraints': { 'audio': this.audio, 'video': this.video }
    };

    this._rtc = this._ua.call(exten.startsWith('sip:') ? exten : 'sip:' + exten + '@' + this.host, options);
};

CyberMegaPhone.prototype.terminate = function () {
    this._locals.removeAll();
    this._remotes.removeAll();
    if (this._ua && this._rtc) {
        this._rtc.terminate();
    }
};

function Streams() {
    EasyEvent.call(this);
    this._streams = [];
}

Streams.prototype = Object.create(EasyEvent.prototype);
Streams.prototype.constructor = Streams;

Streams.prototype.add = function (stream) {
    if (this._streams.indexOf(stream) === -1) {
        this._streams.push(stream);
        console.log('Streams: added ' + stream.id);
        this.raise('streamAdded', stream);
    }
};

Streams.prototype.remove = function (stream) {
    let index = this._streams.indexOf(stream);
    if (index !== -1) {
        let removed = this._streams.splice(index, 1);
        console.log('Streams: removed ' + removed[0].id);
        this.raise('streamRemoved', removed[0]);
    }
};

Streams.prototype.removeAll = function () {
    while (this._streams.length > 0) {
        this.remove(this._streams[0]);
    }
};

function EasyEvent() {
    this._events = {};
}

EasyEvent.prototype.handle = function (name, fun) {
    if (!this._events[name]) {
        this._events[name] = [];
    }
    this._events[name].push(fun);
};

EasyEvent.prototype.raise = function (name, args) {
    if (this._events[name]) {
        this._events[name].forEach(fun => fun.apply(null, args));
    }
};

EasyEvent.prototype.bubble = function (name, obj) {
    this.handle(name, data => {
        obj.raise(name, data);
    });
};

// Functions for handling UI elements for incoming calls
function showIncomingCallModal() {
    document.getElementById("incoming-call-modal").style.display = "block";
}

function closeIncomingCallModal() {
    document.getElementById("incoming-call-modal").style.display = "none";
}

How fix this issue? Why occurring it?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.