Enabling sender-side statistics

To receive sender-side statistics, enable them for the stream’s publisher by setting the senderStatsTrack property to true for the OTPublisherKitSettings object used to create the publisher.

OTPublisherKitSettings *settings = [[OTPublisherKitSettings alloc] init];
settings.senderStatsTrack = YES;

OTPublisher *publisher = [[OTPublisher alloc] initWithDelegate:self
                                                      settings:settings];

If senderStatsTrack is not enabled, no sender statistics channel will be published for this publisher. The default value is NO.

Subscribing to sender-side statistics

Subscribers automatically receive sender-side statistics when you register a listener for either video or audio stats.

Example: enabling video stats subscription with sender-side fields:

// In your .h or class extension: adopt the protocol
@interface MyViewController () <OTSubscriberKitNetworkStatsDelegate>
@end

// When creating the OTSubscriber:
OTSubscriber *subscriber = [[OTSubscriber alloc] initWithStream:stream delegate:self];
subscriber.networkStatsDelegate = self;
NSError *err = nil;
[session subscribe:subscriber error:&err];

Implement the delegate method for video stats:

- (void)subscriber:(OTSubscriberKit *)subscriber
videoNetworkStatsUpdated:(OTSubscriberKitVideoNetworkStats *)stats
{
    NSLog(@"Video packets received: %llu", (unsigned long long)stats.videoPacketsReceived);
    NSLog(@"Video packets lost: %llu", (unsigned long long)stats.videoPacketsLost);
    NSLog(@"Video bytes received: %llu", (unsigned long long)stats.videoBytesReceived);
    NSLog(@"Timestamp (ms): %llu", (unsigned long long)stats.timestamp);

    // The property may be nil if no sender statistics have been received yet.
    if (stats.senderStats) {
        OTSenderStats *sender = stats.senderStats;
        NSLog(@"Connection max allocated bitrate: %lld bps", (long long)sender.connectionMaxAllocatedBitrate);
        NSLog(@"Connection current estimated bandwidth: %lld bps", (long long)sender.connectionEstimatedBandwidth);
    } else {
        NSLog(@"Sender-side stats not available yet.");
    }
}

Similarly implement -subscriber:audioNetworkStatsUpdated: for audio stats, which also includes a senderStats property.

Receiving statistics events

Sender-side statistics are delivered via the OTSubscriberKitNetworkStatsDelegate callbacks for video and audio, as shown above. The OTSenderStats, included as the senderStats member in both OTSubscriberKitVideoNetworkStats and OTSubscriberKitAudioNetworkStats, provides two properties:

  • connectionMaxAllocatedBitrate — The maximum bitrate that can be estimated for the connection
  • connectionEstimatedBandwidth — The current estimated bandwidth for the connection

These two metrics are calculated per audio-video bundle, so the same values appear in both video and audio statistics. Because they reflect the transport rather than individual tracks, the metrics are shared across both audio and video.