Skip to content

Commit ee483da

Browse files
wawesomeNOGUISean-Der
authored andcommitted
Add SCTPTransportStats
1 parent cf5dbbe commit ee483da

File tree

3 files changed

+72
-4
lines changed

3 files changed

+72
-4
lines changed

sctptransport.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,16 +362,20 @@ func (r *SCTPTransport) State() SCTPTransportState {
362362
func (r *SCTPTransport) collectStats(collector *statsReportCollector) {
363363
collector.Collecting()
364364

365-
stats := TransportStats{
365+
stats := SCTPTransportStats{
366366
Timestamp: statsTimestampFrom(time.Now()),
367-
Type: StatsTypeTransport,
367+
Type: StatsTypeSCTPTransport,
368368
ID: "sctpTransport",
369369
}
370370

371371
association := r.association()
372372
if association != nil {
373373
stats.BytesSent = association.BytesSent()
374374
stats.BytesReceived = association.BytesReceived()
375+
stats.SmoothedRoundTripTime = association.SRTT() * 0.001 // convert milliseconds to seconds
376+
stats.CongestionWindow = association.CWND()
377+
stats.ReceiverWindow = association.RWND()
378+
stats.MTU = association.MTU()
375379
}
376380

377381
collector.Collect(stats.ID, stats)

stats.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ func UnmarshalStatsJSON(b []byte) (Stats, error) {
6767
return unmarshalICECandidateStats(b)
6868
case StatsTypeCertificate:
6969
return unmarshalCertificateStats(b)
70+
case StatsTypeSCTPTransport:
71+
return unmarshalSCTPTransportStats(b)
7072
default:
7173
return nil, fmt.Errorf("type: %w", ErrUnknownType)
7274
}
@@ -132,6 +134,9 @@ const (
132134

133135
// StatsTypeCertificate is used by CertificateStats.
134136
StatsTypeCertificate StatsType = "certificate"
137+
138+
// StatsTypeSCTPTransport is used by SCTPTransportStats
139+
StatsTypeSCTPTransport StatsType = "sctp-transport"
135140
)
136141

137142
// MediaKind indicates the kind of media (audio or video)
@@ -1980,3 +1985,53 @@ func unmarshalCertificateStats(b []byte) (CertificateStats, error) {
19801985
}
19811986
return certificateStats, nil
19821987
}
1988+
1989+
// SCTPTransportStats contains information about a certificate used by an SCTPTransport.
1990+
type SCTPTransportStats struct {
1991+
// Timestamp is the timestamp associated with this object.
1992+
Timestamp StatsTimestamp `json:"timestamp"`
1993+
1994+
// Type is the object's StatsType
1995+
Type StatsType `json:"type"`
1996+
1997+
// ID is a unique id that is associated with the component inspected to produce
1998+
// this Stats object. Two Stats objects will have the same ID if they were produced
1999+
// by inspecting the same underlying object.
2000+
ID string `json:"id"`
2001+
2002+
// TransportID is the identifier of the object that was inspected to produce the
2003+
// RTCTransportStats for the DTLSTransport and ICETransport supporting the SCTP transport.
2004+
TransportID string `json:"transportId"`
2005+
2006+
// SmoothedRoundTripTime is the latest smoothed round-trip time value, corresponding to spinfo_srtt defined in [RFC6458]
2007+
// but converted to seconds. If there has been no round-trip time measurements yet, this value is undefined.
2008+
SmoothedRoundTripTime float64 `json:"smoothedRoundTripTime"`
2009+
2010+
// CongestionWindow is the latest congestion window, corresponding to spinfo_cwnd defined in [RFC6458].
2011+
CongestionWindow uint32 `json:"congestionWindow"`
2012+
2013+
// ReceiverWindow is the latest receiver window, corresponding to sstat_rwnd defined in [RFC6458].
2014+
ReceiverWindow uint32 `json:"receiverWindow"`
2015+
2016+
// MTU is the latest maximum transmission unit, corresponding to spinfo_mtu defined in [RFC6458].
2017+
MTU uint32 `json:"mtu"`
2018+
2019+
// UNACKData is the number of unacknowledged DATA chunks, corresponding to sstat_unackdata defined in [RFC6458].
2020+
UNACKData uint32 `json:"unackData"`
2021+
2022+
// BytesSent represents the total number of bytes sent on this SCTPTransport
2023+
BytesSent uint64 `json:"bytesSent"`
2024+
2025+
// BytesReceived represents the total number of bytes received on this SCTPTransport
2026+
BytesReceived uint64 `json:"bytesReceived"`
2027+
}
2028+
2029+
func (s SCTPTransportStats) statsMarker() {}
2030+
2031+
func unmarshalSCTPTransportStats(b []byte) (SCTPTransportStats, error) {
2032+
var sctpTransportStats SCTPTransportStats
2033+
if err := json.Unmarshal(b, &sctpTransportStats); err != nil {
2034+
return SCTPTransportStats{}, fmt.Errorf("unmarshal sctp transport stats: %w", err)
2035+
}
2036+
return sctpTransportStats, nil
2037+
}

stats_go_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,15 @@ func getTransportStats(t *testing.T, report StatsReport, statsID string) Transpo
10691069
return transportStats
10701070
}
10711071

1072+
func getSctpTransportStats(t *testing.T, report StatsReport) SCTPTransportStats {
1073+
stats, ok := report["sctpTransport"]
1074+
assert.True(t, ok)
1075+
transportStats, ok := stats.(SCTPTransportStats)
1076+
assert.True(t, ok)
1077+
assert.Equal(t, transportStats.Type, StatsTypeSCTPTransport)
1078+
return transportStats
1079+
}
1080+
10721081
func getCertificateStats(t *testing.T, report StatsReport, certificate *Certificate) CertificateStats {
10731082
certificateStats, ok := report.GetCertificateStats(certificate)
10741083
assert.True(t, ok)
@@ -1314,8 +1323,8 @@ func TestPeerConnection_GetStats(t *testing.T) {
13141323
assert.GreaterOrEqual(t, offerICETransportStats.BytesSent, answerICETransportStats.BytesReceived)
13151324
assert.GreaterOrEqual(t, answerICETransportStats.BytesSent, offerICETransportStats.BytesReceived)
13161325

1317-
answerSCTPTransportStats := getTransportStats(t, reportPCAnswer, "sctpTransport")
1318-
offerSCTPTransportStats := getTransportStats(t, reportPCOffer, "sctpTransport")
1326+
answerSCTPTransportStats := getSctpTransportStats(t, reportPCAnswer)
1327+
offerSCTPTransportStats := getSctpTransportStats(t, reportPCOffer)
13191328
assert.GreaterOrEqual(t, offerSCTPTransportStats.BytesSent, answerSCTPTransportStats.BytesReceived)
13201329
assert.GreaterOrEqual(t, answerSCTPTransportStats.BytesSent, offerSCTPTransportStats.BytesReceived)
13211330

0 commit comments

Comments
 (0)