Peer to peer call with user accept/refuse

Here is a description of the different steps to add user accept / refuse on incoming calls. This can be applied on audio, video, screensharing or datachannel calls.

Establish a call

Check our tutorial on call establishment if needed.

Audio/Video calls : Add accept / refuse processing on "session#incomingCall" event

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

        connectedSession
            .on('incomingCall', function (invitation) {
                callInvitationProcess(invitation);
            });
    

ScreenSharing calls

Activate ScreenSharing calls invitation

Default behavior of ApiRTC is to auto-accept screensharing calls as stream is unidirectional. Add 'userAcceptOnIncomingScreenSharingCall' parameter on userAgent#register() to enable the screenSharingCallInvitation event.
        ua.register({
            userAcceptOnIncomingScreenSharingCall : true
        })
    

ScreenSharing calls : Add accept / refuse processing on "session#incomingScreenSharingCallInvitation" event

Add listener on 'session#incomingScreenSharingCallInvitation' to receive screenSharing calls invitation. Note the difference with 'incomingScreenSharingCall' event that is used when 'userAcceptOnIncomingScreenSharingCall' is not activated.

        connectedSession
            .on("incomingScreenSharingCallInvitation", function (invitation) { //When client receives an screenSharing call from another user
                callInvitationProcess(invitation);
            });
    

add Invitation Processing

Add listener on 'invitation#statusChange' to handle the case when user cancel his call. Add invitation.accept() or invitation.decline() action on buttons.

        function callInvitationProcess(invitation) {
            invitation.on("statusChange", function (statusChangeInfo) {
                console.error('statusChangeInfo :', statusChangeInfo);

                if (statusChangeInfo.status === apiRTC.INVITATION_STATUS_EXPIRED) {

                    console.error('INVITATION_STATUS_EXPIRED');
                    // Hide accept/decline buttons
                    hideAcceptDeclineButtons();
                }
            });
            //===============================================
            // ACCEPT OR DECLINE
            //===============================================
            // Display accept/decline buttons
            showAcceptDeclineButtons();

            // Add listeners
            $("#accept").click(function () {
                //==============================
                // ACCEPT CALL INVITATION
                //==============================
                invitation.accept()
                    .then(function(call) {
                        setCallListeners(call);
                        addHangupButton(call.getId());
                    });
                // Hide accept/decline buttons
                hideAcceptDeclineButtons();
            });
            $("#decline").click(function () {
                // Decline call invitation
                invitation.decline();
                // Hide accept/decline buttons
                hideAcceptDeclineButtons();
            });
        }
    

Live Demo