calculateVideoBitrate method

void calculateVideoBitrate(
  1. CubeStatsReport report
)

Implementation

void calculateVideoBitrate(CubeStatsReport report) {
  for (var statsReport in report.stats) {
    var timeStamp;
    var finalBytesReceived;
    var trackId;
    var trackIdentifier;

    if (statsReport.type == 'inbound-rtp') {
      if (MEDIA_TYPE_VIDEO == statsReport.values['mediaType'] ||
          MEDIA_TYPE_VIDEO == statsReport.values['kind']) {
        var bytesReceived = statsReport.values['bytesReceived'];

        finalBytesReceived = int.parse(bytesReceived.toString());
        timeStamp = statsReport.timestamp.floor();
        trackId = statsReport.values['trackId']?.toString();
        trackIdentifier = statsReport.values['trackIdentifier']?.toString();
      }
    } else if (statsReport.type == 'ssrc') {
      if (MEDIA_TYPE_VIDEO == statsReport.values['mediaType']) {
        var bytesReceived = statsReport.values['bytesReceived'];

        if (finalBytesReceived == null && bytesReceived != null) {
          finalBytesReceived = int.parse(bytesReceived.toString());
          timeStamp = statsReport.timestamp.floor();
          trackId ??= statsReport.values['trackId']?.toString();
          trackIdentifier ??= statsReport.values['trackIdentifier']?.toString();
        }
      }
    } else if (statsReport.type == 'media-source') {
      if (MEDIA_TYPE_VIDEO == statsReport.values['kind']) {
        var bytesReceived = statsReport.values['bytesReceived'];

        if (finalBytesReceived == null && bytesReceived != null) {
          finalBytesReceived = int.parse(bytesReceived.toString());
          timeStamp = statsReport.timestamp.floor();
          trackId ??= statsReport.values['trackId']?.toString();
          trackIdentifier ??= statsReport.values['trackIdentifier']?.toString();
        }
      }
    } else if (statsReport.type == 'track') {}

    if (finalBytesReceived != null && finalBytesReceived != 0) {
      var userId = trackId == null && trackIdentifier == null
          ? report.userId
          : callSession?.getUserIdForStream(
              trackId, trackIdentifier, report.userId);

      if (userId != null) {
        var previousTimestamp = _lastUserTimeStamps[userId];
        var previousBytesReceived = _lastUserBytesReceived[userId];

        if (!kIsWeb) {
          timeStamp = timeStamp ~/ 1000;
        }

        if (previousTimestamp == null || previousBytesReceived == null) {
          _lastUserTimeStamps[userId] = timeStamp;
          _lastUserBytesReceived[userId] = finalBytesReceived;
        } else if (finalBytesReceived >= previousBytesReceived) {
          var timePassed = timeStamp - previousTimestamp;

          var bitRate;

          try {
            bitRate = (((finalBytesReceived - previousBytesReceived) * 8) /
                    timePassed)
                .floor();

            _lastUserTimeStamps[userId] = timeStamp;
            _lastUserBytesReceived[userId] = finalBytesReceived;

            _videoBitrateStreamController
                .add(CubeVideoBitrateEvent(userId, bitRate));
          } catch (e) {
            log('ERROR during calculate bitrate from statId ${statsReport.id}, error: $e  ',
                TAG);
          }
        }
      }
    }
  }
}