Skip to content
Merged
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
76 changes: 67 additions & 9 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 @@ -282,6 +294,17 @@ PeerConnectionAnalyzer.prototype = {
return
}

// 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([])
}

if (this._peerDirection === PEER_DIRECTION.SENDER) {
this._processSenderStats(stats)
} else if (this._peerDirection === PEER_DIRECTION.RECEIVER) {
Expand Down Expand Up @@ -353,6 +376,8 @@ PeerConnectionAnalyzer.prototype = {
}

if (stat.type === 'outbound-rtp') {
this._rtcStats[stat.kind].at(-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].at(-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 @@ -441,6 +468,8 @@ PeerConnectionAnalyzer.prototype = {
}

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

if ('packetsReceived' in stat && 'kind' in stat) {
packetsReceived[stat.kind] = stat.packetsReceived
}
Expand All @@ -450,6 +479,10 @@ PeerConnectionAnalyzer.prototype = {
if ('timestamp' in stat && 'kind' in stat) {
timestamp[stat.kind] = stat.timestamp
}
} else if (stat.type === 'remote-outbound-rtp') {
// Even if the stat is not used in calculations it is logged for
// additional context.
this._rtcStats[stat.kind].at(-1).push(stat)
}
}

Expand Down Expand Up @@ -724,27 +757,52 @@ 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)
console.debug('%s: %s', tag, message)
}

console.debug(tag + 'Packets: ' + this._packets[kind].toString())
console.debug(tag + 'Packets lost: ' + this._packetsLost[kind].toString())
console.debug(tag + 'Packets lost ratio: ' + this._packetsLostRatio[kind].toString())
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())
console.debug('%s: Packets: %s', tag, this._packets[kind].toString())
console.debug('%s: Packets lost: %s', tag, this._packetsLost[kind].toString())
console.debug('%s: Packets lost ratio: %s', tag, this._packetsLostRatio[kind].toString())
console.debug('%s: Packets per second: %s', tag, this._packetsPerSecond[kind].toString())
console.debug('%s: Round trip time: %s', tag, this._roundTripTime[kind].toString())
console.debug('%s: Timestamps: %s', tag, this._timestampsForLogs[kind].toString())

this._logRtcStats(tag, kind)
},

_logRtcStats(tag, kind) {
this._rtcStats[kind].forEach((rtcStats, i) => {
if (!rtcStats.length) {
console.debug('%s: %i: no matching type', tag, i)
return
}

rtcStats.forEach((rtcStat, j) => {
console.debug('%s: %i-%i: %s', tag, i, j, JSON.stringify(rtcStat))
})
})
}

}

EmitterMixin.apply(PeerConnectionAnalyzer.prototype)

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