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()

        session.onEvent(self, { (event) in
            switch event {
            case .incomingCall(let invitation):
                incoming.accept(completion: { (error, call) in
                    if let error = error {
                        showError(error)
                        return
                    }
                    guard let call = call else {
                        showError("Call is nil")
                        return
                    }
            })
            default:
                break
            }
        })
    

Decline invitation

You can decline an invitation with the following code.

        incomingInvitation?.decline(completion: { (error) in
            if let error = error {
                showError(error)
                return
            }
        })
    

Establish a call

        session.getContact(id: contactId).call { (error, call) in
            if let error = error {
                self.state = .def
                showError(error)
                return
            }
            guard let call = call else {
                self.state = .def
                showError("Call is nil")
                return
            }
            self.activeCall = call
        }
    

Add events listeners on Call to be informed of related events

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

        call.onEvent(self) { [weak self] (event) in
            guard let `self` = self else {
                return
            }
            switch event {
            case .hangup:
                self.handleRemoteHangUp()
            case .localStreamAvailable(let localStream):
                self.handleNewLocalStream(localStream)
            case .streamAdded(let remoteStream):
                self.handleNewRemoteStream(remoteStream)
            case .streamRemove(let removeStream):
                self.handleRemoveStream(removeStream)
            case .error(let error):
                showError(error)
            default:
                break
            }
        }
    

Add possibility to hangup the call

        activeCall?.hangUp(completion: { [weak self] in
            self?.activeCall = nil
        })