Talk to us
Talk to us
menu

Blog Post

start building
Developer

How To Implement Call Duration Statistics Function

How To Implement Call Duration Statistics Function

The call duration refers to the course from the start to the end of the call. It’s an essential function of audio and video calls, commonly used in:

  • Real-time display of call duration;
  • Call duration record;
  • For the call billing;

This article will explain how to implement this functionality in your App.

Call duration formula

The calculation formula for call duration is as follows:

Call duration = call end timestamp - call start timestamp

You only need to get the call start timestamp and call end timestamp to calculate the call duration.

However, there are some exceptions. For example, in the case of a network interruption or app crash, how to ensure the accuracy of call duration calculation?

Furthermore, how to ensure that the billing logic is safe and that there is no manipulation?

Let’s see how to tackle these issues.

Real-time display of call duration

call duration

The call duration function is often implemented on the client side. Real-time and fluency of timing updates need special attention.

The processing logic is as such:

  • the App listens to the call start event
  • The timer starts, polls once per second, and adds 1 second to the call duration
call duration display

ZEGOCLOUD UIKits determines when the user starts and ends a call by monitoring the callback events of the user joining and exiting the call room.

Implement the real-time display of call duration

ZEGOCLOUD UIKits provides RoomStateChanged callback notification. In particular, you can get notified when:

  • Joining a call room.
  • Being kicked out of the call room.
  • Exiting the call room.

We can get the start and end timestamp of the call through it.

1. Add event listener

ZegoUIKit.shared.addEventHandler(self)

Please note that listeners can be added by configuring ZegoUIKitPrebuiltCallInvitationService. This way, the logic of UIKits is processed first, followed by the logic you added without errors.

2. Callback event handling

In the RoomStateChanged callback event, we need to handle two types of events:

  1. call start
  2. call end.

The call start implements the following logic:

  • View to display call duration.
  • Start the timer

The call end implements:

  • Stop the timer.
extension ViewController: ZegoUIKitEventHandle {
    func onRoomStateChanged(_ reason: ZegoUIKitRoomStateChangedReason, errorCode: Int32, extendedData: [AnyHashable : Any], roomID: String) {

        switch reason {
        case .logined:
            addTimerLabel()
            startTimer()
            break
        case .logout, .kickOut,.reconnectFailed:
            stopTimer()
            break
        default:
            break
        }
    }
}

2.1 Added view to display call duration

private func addTimerLabel() {
        timerLabel.text = ""
        ZegoUIKitPrebuiltCallInvitationService.shared.callVC?.view.addSubview(timerLabel)
        let width = UIScreen.main.bounds.width
        timerLabel.frame = CGRect(x: 0, y: 80, width: width, height: 50)
        timerLabel.textColor = .white
        timerLabel.textAlignment = .center
    }

2.2 Start timer

var timer: Timer?
private func startTimer() {
        var time = 0

        let newTimer = Timer(timeInterval: 1.0, repeats: true) { timer in
            time = time + 1
            self.timerLabel.text = self.transToHourMinSec(time: time)
        }
        RunLoop.current.add(newTimer, forMode: RunLoop.Mode.default)
        newTimer.fire()
        timer = newTimer
    }

2.3 Stop timer

private func stopTimer() {
        timer?.invalidate()
}

Call recording and call billing

These two are very different from real-time call duration. The duration counted in the call history is used as a billing voucher. Accuracy and security are of utmost importance.

Business servers implement these types of functions. ZEGOCLOUD UIKits provides server event callbacks for all users joining and leaving the call room so that you can get the ‘start timestamp’ and ‘end timestamp’ of the call.

Let’s see each scenario.

call recorder

1. Call ended normally

If the call process is initiated and ended by the user, the call duration counted by your business server will be consistent with the call duration recorded by the user.

call ended normally


2. Call ended abnormally

If the call ends due to network disconnection or application process termination, it results in a longer duration, according to the business server. There are two ways to restore the accuracy of call duration, there are two ways:

  • Subtract the reconnect time from the total time.
  • Set a shorter reconnecting time through the SDK API.
call ended abnormally

Implement call billing

The business server implements the logic responsible for the call billing.

Whether it is a normal call scene or an abnormal call scene, the server monitors the Room logged in and Room logged out callback events.

1. Add callback listener

The ZEGOCLOUD console provides callback monitoring settings. You only need to set the API you use for monitoring on the console to receive callback events. For detailed tutorials, please refer to the following documents:

2. Call exception handling

2.1 Call ended abnormally

call ended abnormally

As shown in the figure, when the application is disconnected or exited, ZEGOCLOUD Real-Time NetWord will wait for the application to reconnect. After reconnecting fails, it will send a room logged out callback to your server. Therefore, the reconnection time will also be counted into the billing time.

You can determine the cause of the abnormal logout in the Reason Field. If it’s a heartbeat timeout, you can subtract the reconnection time. Our default reconnection time is 90 seconds.

heartbeat timeout

2.2 Call balance exhausted

In case of a time-charged call, there might be an insufficient balance situation. In this case, it is necessary to remind the user to top up or kick the user out of the calling room.

When the user balance is insufficient, you can force him/her to end the call by calling the ZEGOCLOUD server API Kick a user out of a room API.

call balance exhausted

Conclusion

We have seen together the commonly used calculation method for call duration. However, the call duration will vary according to different business scenarios and billing methods.

Of course, in case of necessity, you can contact us. We will formulate the best plan for you.

Read more

ZEGOCLOUD
ZEGOCLOUD With our fully customizable and easy-to-integrate Live SDK, you can quickly build reliable, scalable, and interactive live streaming into your mobile, web, and desktop apps.

Related posts

Contact us