Enabling sender-side statistics
To receive sender-side statistics, enable them for the stream’s publisher by setting the HasSenderStatsTrack property to true when building the publisher:
var publisherBuilder = new Publisher.Builder()
{
HasSenderStatsTrack = true
};
Publisher publisher = publisherBuilder.Build();
If HasSenderStatsTrack is not enabled, no sender statistics channel will be published for this publisher. The default value is false.
Subscribing to sender-side statistics
Subscribers automatically receive sender statistics only if the publisher has enabled them and if the subscriber registers a listener for network statistics events.
You can subscribe to video and audio stats using the corresponding event handlers:
subscriber.VideoStatsUpdated += (sender, stats) =>
{
// Received video stats
};
subscriber.AudioStatsUpdated += (sender, stats) =>
{
// Received audio stats
};
Receiving statistics events
Sender-side statistics are delivered via the VideoStatsUpdated and AudioStatsUpdated events for video and audio. The SenderStats class, included in both VideoNetworkStatsEventArgs and AudioNetworkStatsEventArgs, provides two properties:
ConnectionMaxAllocatedBitrate— The maximum bitrate that can be estimated for the connectionConnectionEstimatedBandwidth— 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.
Inside the stats event handlers, you can access the sender-side metrics through the optional SenderStats property:
subscriber.VideoStatsUpdated += (sender, stats) =>
{
Console.WriteLine($"Packets received: {stats.PacketsReceived}");
Console.WriteLine($"Packets lost: {stats.PacketsLost}");
Console.WriteLine($"Bytes received: {stats.BytesReceived}");
Console.WriteLine($"Timestamp: {stats.Timestamp}");
if (stats.SenderStats != null)
{
Console.WriteLine($"Connection max allocated bitrate: {stats.SenderStats.ConnectionMaxAllocatedBitrate}");
Console.WriteLine($"Connection current estimated bandwidth: {stats.SenderStats.ConnectionEstimatedBandwidth}");
}
else
{
Console.WriteLine("Sender stats not available yet.");
}
};
The same approach applies to audio stats using the AudioStatsUpdated event.