Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more information to PeerConnectionAnalyzer logs #14442

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/utils/webrtc/analyzers/ParticipantAnalyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { PEER_DIRECTION, PeerConnectionAnalyzer } from './PeerConnectionAnalyzer.js'
import {
PEER_DIRECTION,
PEER_TYPE,
PeerConnectionAnalyzer
} from './PeerConnectionAnalyzer.js'
import EmitterMixin from '../../EmitterMixin.js'

/**
Expand Down Expand Up @@ -201,7 +205,7 @@ ParticipantAnalyzer.prototype = {

_startListeningToScreenChanges() {
if (this._localMediaModel) {
this._senderScreenPeerConnectionAnalyzer.setPeerConnection(this._screenPeer.pc, PEER_DIRECTION.SENDER)
this._senderScreenPeerConnectionAnalyzer.setPeerConnection(this._screenPeer.pc, PEER_DIRECTION.SENDER, PEER_TYPE.SCREEN)

this._senderScreenPeerConnectionAnalyzer.on('change:connectionQualityVideo', this._handleConnectionQualityScreenChangeBound)

Expand All @@ -210,7 +214,7 @@ ParticipantAnalyzer.prototype = {
}

if (this._callParticipantModel) {
this._receiverScreenPeerConnectionAnalyzer.setPeerConnection(this._screenPeer.pc, PEER_DIRECTION.RECEIVER)
this._receiverScreenPeerConnectionAnalyzer.setPeerConnection(this._screenPeer.pc, PEER_DIRECTION.RECEIVER, PEER_TYPE.SCREEN)

this._receiverScreenPeerConnectionAnalyzer.on('change:connectionQualityVideo', this._handleConnectionQualityScreenChangeBound)

Expand Down
73 changes: 66 additions & 7 deletions src/utils/webrtc/analyzers/PeerConnectionAnalyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ const PEER_DIRECTION = {
RECEIVER: 1,
}

const PEER_TYPE = {
VIDEO: 0,
SCREEN: 1,
}

/**
* Analyzer for the quality of the connection of an RTCPeerConnection.
*
Expand Down Expand Up @@ -56,6 +61,11 @@ const PEER_DIRECTION = {
function PeerConnectionAnalyzer() {
this._superEmitterMixin()

this._rtcStats = {
audio: [],
video: [],
}

this._packets = {
audio: new AverageStatValue(5, STAT_VALUE_TYPE.CUMULATIVE),
video: new AverageStatValue(5, STAT_VALUE_TYPE.CUMULATIVE),
Expand Down Expand Up @@ -114,6 +124,7 @@ function PeerConnectionAnalyzer() {

this._peerConnection = null
this._peerDirection = null
this._peerType = null

this._getStatsInterval = null

Expand Down Expand Up @@ -154,7 +165,7 @@ PeerConnectionAnalyzer.prototype = {
this._trigger('change:connectionQualityVideo', [connectionQualityVideo])
},

setPeerConnection(peerConnection, peerDirection = null) {
setPeerConnection(peerConnection, peerDirection = null, peerType = PEER_TYPE.VIDEO) {
if (this._peerConnection) {
this._peerConnection.removeEventListener('iceconnectionstatechange', this._handleIceConnectionStateChangedBound)
this._peerConnection.removeEventListener('connectionstatechange', this._handleConnectionStateChangedBound)
Expand All @@ -163,6 +174,7 @@ PeerConnectionAnalyzer.prototype = {

this._peerConnection = peerConnection
this._peerDirection = peerDirection
this._peerType = peerType

this._setConnectionQualityAudio(CONNECTION_QUALITY.UNKNOWN)
this._setConnectionQualityVideo(CONNECTION_QUALITY.UNKNOWN)
Expand Down Expand Up @@ -297,6 +309,17 @@ PeerConnectionAnalyzer.prototype = {
},

_processSenderStats(stats) {
// Although the last five stats are analyzed a few more RTC stats are
// kept to provide an extended context in the logs.
const NUMBER_OF_RTC_STATS_TO_KEEP = 7

for (const kind of ['audio', 'video']) {
if (this._rtcStats[kind].length === NUMBER_OF_RTC_STATS_TO_KEEP) {
this._rtcStats[kind].shift()
}
this._rtcStats[kind].push([])
}

// Packets are calculated as "packetsReceived + packetsLost" or as
// "packetsSent" depending on the browser (see below).
const packets = {
Expand Down Expand Up @@ -353,6 +376,8 @@ PeerConnectionAnalyzer.prototype = {
}

if (stat.type === 'outbound-rtp') {
this._rtcStats[stat.kind][this._rtcStats[stat.kind].length - 1].push(stat)

if ('packetsSent' in stat && 'kind' in stat) {
packetsSent[stat.kind] = (packetsSent[stat.kind] === -1) ? stat.packetsSent : packetsSent[stat.kind] + stat.packetsSent

Expand All @@ -361,6 +386,8 @@ PeerConnectionAnalyzer.prototype = {
}
}
} else if (stat.type === 'remote-inbound-rtp') {
this._rtcStats[stat.kind][this._rtcStats[stat.kind].length - 1].push(stat)

if ('packetsReceived' in stat && 'kind' in stat) {
packetsReceived[stat.kind] = (packetsReceived[stat.kind] === -1) ? stat.packetsReceived : packetsReceived[stat.kind] + stat.packetsReceived

Expand Down Expand Up @@ -506,11 +533,10 @@ PeerConnectionAnalyzer.prototype = {

this._stageStats(kind, packets, packetsLost, timestamp, roundTripTime)

// If the packets have changed now it is assumed that the previous stats
// were stalled.
if (packets > 0) {
this._distributeStagedStats(kind)
}
// Distributing the stats has no effect if the stats were not stalled
// (that is, if the values are still unchanged, so it is probably an
// actual connection problem rather than a stalled report).
this._distributeStagedStats(kind)

while (this._stagedPackets[kind].length > 0) {
const stagedPackets = this._stagedPackets[kind].shift()
Expand Down Expand Up @@ -550,6 +576,12 @@ PeerConnectionAnalyzer.prototype = {
let packetsLostTotal = 0
let timestampsTotal = 0

// If the last timestamp is still stalled there is nothing to
// distribute.
if (this._stagedTimestamps[kind][this._stagedTimestamps[kind].length - 1] === timestampsBase) {
return
}

// If the first timestamp stalled it is assumed that all of them
// stalled and are thus evenly distributed based on the new timestamp.
if (this._stagedTimestamps[kind][0] === timestampsBase) {
Expand Down Expand Up @@ -719,8 +751,17 @@ PeerConnectionAnalyzer.prototype = {
return CONNECTION_QUALITY.GOOD
},

_getLogTag(kind) {
let type = kind
if (this._peerType === PEER_TYPE.SCREEN) {
type += ' (screen)'
}

return 'PeerConnectionAnalyzer: ' + type + ': '
},

_logStats(kind, message) {
const tag = 'PeerConnectionAnalyzer: ' + kind + ': '
const tag = this._getLogTag(kind)

if (message) {
console.debug(tag + message)
Expand All @@ -732,14 +773,32 @@ PeerConnectionAnalyzer.prototype = {
console.debug(tag + 'Packets per second: ' + this._packetsPerSecond[kind].toString())
console.debug(tag + 'Round trip time: ' + this._roundTripTime[kind].toString())
console.debug(tag + 'Timestamps: ' + this._timestampsForLogs[kind].toString())

this._logRtcStats(kind)
},

_logRtcStats(kind) {
const tag = this._getLogTag(kind)

for (const rtcStats of this._rtcStats[kind]) {
if (!rtcStats.length) {
console.debug(tag + 'no matching type')
continue
}

for (const rtcStat of rtcStats) {
console.debug(tag + JSON.stringify(rtcStat))
}
}
}

}

EmitterMixin.apply(PeerConnectionAnalyzer.prototype)

export {
CONNECTION_QUALITY,
PEER_DIRECTION,
PEER_TYPE,
PeerConnectionAnalyzer,
}
Loading