Peer to peer call

Here is a description of the different steps to create a peer to peer call using apiRTC

Create and register a UserAgent

Check our tutorial on registration if needed to create the userAgent and register.

Add events listeners on Session to enable incoming calls

Add listener on 'incomingCall' to receive audio/video calls and on 'incomingScreenSharingCall' to receive screenSharing calls. As describe in following sample, to accept the call use invitation.accept()

        .on('incomingCall', function (invitation) {
            console.log("MAIN - incomingCall");
            //==============================
            // ACCEPT CALL INVITATION
            //==============================
            invitation.accept()
                .then(function (call) {
                    setCallListeners(call);
                    addHangupButton(call.getId());
                });
            // Display hangup button
            document.getElementById('hangup').style.display = 'inline-block';
        })
        .on("incomingScreenSharingCall", function (call) { //When client receives an screenSharing call from another user
            console.log("screenSharing received from :", call.getContact().id);
            setCallListeners(call);
            addHangupButton(call.getId());
        });
    

Establish a call

        var contact = connectedSession.getOrCreateContact($("#number").val());
        var call = contact.call();
    

Add events listeners on Call to be informed of related events

To manage call events add following listeners : - localStreamAvailable - streamAdded - streamRemoved - userMediaError - desktopCapture - hangup

        .on("localStreamAvailable", function (stream) {
            console.log('localStreamAvailable');
            //document.getElementById('local-media').remove();
            addStreamInDiv(stream, 'local-container', 'local-media-' + stream.getId(), {width : "160px", height : "120px"}, true);
            stream
                .on("stopped", function () { //When client receives an screenSharing call from another user
                    console.error("Stream stopped");
                    $('#local-media-' + stream.getId()).remove();
                });
        })
        .on("streamAdded", function (stream) {
            console.log('stream :', stream);
            addStreamInDiv(stream, 'remote-container', 'remote-media-' + stream.getId(), {width : "640px", height : "480px"}, false);
        })
        .on('streamRemoved', function (stream) {
            // Remove media element
            document.getElementById('remote-media-' + stream.getId()).remove();
        })
        .on('userMediaError', function (e) {
            console.log('userMediaError detected : ', e);
            console.log('userMediaError detected with error : ', e.error);

            //Checking if tryAudioCallActivated
            if (e.tryAudioCallActivated === false) {
                $('#hangup-' + call.getId()).remove();
            }
        })
        .on('desktopCapture', function (e) {
            console.log('desktopCapture event : ', e);
            $('#hangup-' + call.getId()).remove();
        })
        .on('hangup', function () {
            $('#hangup-' + call.getId()).remove();
        });
    

Add possibility to hangup the call

    var call = connectedSession.getCall(callId);
    call.hangUp();
    

Live Demo