monitorStats method

  1. @override
Future<bool> monitorStats()
override

Implementation

@override
Future<bool> monitorStats() async {
  if (sender == null || events.isDisposed) {
    _currentBitrate = 0;
    return false;
  }
  List<VideoSenderStats> stats = [];
  try {
    stats = await getSenderStats();
  } catch (e) {
    logger.warning('Failed to get sender stats: $e');
    return false;
  }
  Map<String, VideoSenderStats> statsMap = {};

  for (var s in stats) {
    statsMap[s.rid ?? 'f'] = s;
  }

  if (prevStats != null) {
    num totalBitrate = 0;
    statsMap.forEach((key, s) {
      final prev = prevStats![key];
      var bitRateForlayer = computeBitrateForSenderStats(s, prev).toInt();
      _bitrateFoLayers[key] = bitRateForlayer;
      totalBitrate += bitRateForlayer;
    });
    _currentBitrate = totalBitrate;
    events.emit(VideoSenderStatsEvent(
      stats: statsMap,
      currentBitrate: currentBitrate,
      bitrateForLayers: _bitrateFoLayers,
    ));
  }

  prevStats = statsMap;
  return true;
}