General VIDEO call flow:
- When Device A will call over VoIP, using ‘makeCall’ then twillio will fire a push notification to Device B and Device A will get connected to room for video call and will wait for peer participant to get connected.
- If Device B accepts the call, then we need to disconnect the VoIP call and then connect to Video Call in the same room in which Device A is connected.
- So, when device B accepts the call, we need to disconnect the VoIP call. Call disconnect listener will be called in Device A end and it will look for any participant is added in Video Call room and will wait for 10 sec. Under this 10 second if peer device is getting connected then we can have a normal video call. And if peer device B is not getting connected in 10 second then automatically call will be disconnected. It will also be helpful when receiver (device B) rejects the call, Caller call will be rejected in 10 seconds.
- Twillio Video call provides feature like group call, change audio device, camera switch and mute.
- Any unique name will work as room name, here we have kept logic for room name like:
IdentityA_IdentityB or userNameA_userNameB.
-
- Caller Side
- Receiver Side
- Caller Side Implementation:
-
- After configuring VoIP call code, open video call screen with camera preview window.
-
- Other configuration for video call like creating audio, video tracks, access token configuration these all configuration is done after the other person picks up the call. This is to avoid audio session related issue that can occur because two libraries(TwilioVoice and TwilioVideo) that are using AudioSession.
- To know when user disconnects call, there is one method in TVOCallDelegate, call(_ call: TVOCall, didDisconnectWithError error: Error?). This method gets called when VoIP call disconnects.
- So at receiver’s end VoIP call needs to get disconnected after answering or declining the call. Logic for how call disconnects at receiver side will be described later.
- After call gets disconnected we can start configuration of access token or audio and video track and other necessary configuration for caller side that are described in quickstart guide of video call and in its documentation.
- We can identify from roomDidConnect(room: Room) method of RoomDelegate to check if user has successfully connected to the room.
-
- As caller enters in the room for video call, he will wait here for 10 seconds to see if anyone enters the room or not. We can do this by setting a boolean flag that is true if we are getting an event on didSubscribeToAudioTrack(audioTrack: RemoteAudioTrack, publication: RemoteAudioTrackPublication, participant: RemoteParticipant) or didSubscribeToVideoTrack(videoTrack: RemoteVideoTrack, publication: RemoteVideoTrackPublication, participant: RemoteParticipant) that means some user has joined the video call. These methods are from RemoteParticipantDelegate.
- Whenever you want to disconnect video call use self.room?.disconnect() method.
2. Receiver Side Implementation:
-
- When receiver receives the VoIP call, he has two options: 1) Answer 2) Decline
- When receiver answers the call performAnswerVoiceCall(uuid: UUID, completionHandler: @escaping (Bool) -> Void) gets called so we can set a boolean flag for call answered to true. We need to set another boolean flag for call disconnected to false in this method only. This call disconnection flag checks if VoIP call disconnected or not.
-
- Whenever user picks up the call after that application will come to the foreground so register a notification UIApplication.didBecomeActiveNotification to implement code for call disconnection when app comes to foreground.
- In the method that will get called when app comes to background, check for below three things:
-
-
- User must be receiver (We can check this by setting a boolean flag when user makes call it is true otherwise its false)
- VoIP call must be active. This means call is not yet disconnected. To check this we can use activeCall object already available in quickstart project for VoIP. If this object is not equal to nil means call is not yet disconnected.
- We have set one boolean flag in performAnswerVoiceCall method to check for call disconnection. This flag must be false that means call is yet not disconnected.
-
- When all these three condition meets then only we can run the code for call disconnection by calling the method performEndCallAction(uuid: activeCall!.uuid).
- When call disconnected successfully, we can start video call configuration.
- Now when user declines the call caller will get call disconnection delegate event as we discussed in caller scenario.