From b5efa6c01497a66575152dff5abd3f057b11557d Mon Sep 17 00:00:00 2001 From: boks1971 Date: Mon, 7 Oct 2024 13:26:30 +0530 Subject: [PATCH] Add ice transport api to get selected pair stats In use cases like SFU, it is useful to get just the selected candidate pair stats to have access to current RTT on the peer connection. The standard has a way to do `GetSelectedCandidatePair` on `ICETransport`, but does not have a way to get stats of that pair. Although not in standard, adding a method to `ICETransport` to get selected candidate pair along similar lines of above method. --- go.mod | 2 +- go.sum | 4 ++-- icegatherer.go | 51 ++++++++++++++++++++++++++++++++++++++++++++ icetransport.go | 6 ++++++ icetransport_test.go | 8 +++++++ 5 files changed, 68 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 5c4ff453e99..6b343a6f822 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.20 require ( github.com/pion/datachannel v1.5.9 github.com/pion/dtls/v3 v3.0.3 - github.com/pion/ice/v4 v4.0.1 + github.com/pion/ice/v4 v4.0.2 github.com/pion/interceptor v0.1.37 github.com/pion/logging v0.2.2 github.com/pion/randutil v0.1.0 diff --git a/go.sum b/go.sum index 7155062e611..25205ed30ef 100644 --- a/go.sum +++ b/go.sum @@ -39,8 +39,8 @@ github.com/pion/datachannel v1.5.9 h1:LpIWAOYPyDrXtU+BW7X0Yt/vGtYxtXQ8ql7dFfYUVZ github.com/pion/datachannel v1.5.9/go.mod h1:kDUuk4CU4Uxp82NH4LQZbISULkX/HtzKa4P7ldf9izE= github.com/pion/dtls/v3 v3.0.3 h1:j5ajZbQwff7Z8k3pE3S+rQ4STvKvXUdKsi/07ka+OWM= github.com/pion/dtls/v3 v3.0.3/go.mod h1:weOTUyIV4z0bQaVzKe8kpaP17+us3yAuiQsEAG1STMU= -github.com/pion/ice/v4 v4.0.1 h1:2d3tPoTR90F3TcGYeXUwucGlXI3hds96cwv4kjZmb9s= -github.com/pion/ice/v4 v4.0.1/go.mod h1:2dpakjpd7+74L5j3TAe6gvkbI5UIzOgAnkimm9SuHvA= +github.com/pion/ice/v4 v4.0.2 h1:1JhBRX8iQLi0+TfcavTjPjI6GO41MFn4CeTBX+Y9h5s= +github.com/pion/ice/v4 v4.0.2/go.mod h1:DCdqyzgtsDNYN6/3U8044j3U7qsJ9KFJC92VnOWHvXg= github.com/pion/interceptor v0.1.37 h1:aRA8Zpab/wE7/c0O3fh1PqY0AJI3fCSEM5lRWJVorwI= github.com/pion/interceptor v0.1.37/go.mod h1:JzxbJ4umVTlZAf+/utHzNesY8tmRkM2lVmkS82TTj8Y= github.com/pion/logging v0.2.2 h1:M9+AIj/+pxNsDfAT64+MAVgJO0rsyLnoJKCqf//DoeY= diff --git a/icegatherer.go b/icegatherer.go index 06b1df3d15e..e8d0f363c5e 100644 --- a/icegatherer.go +++ b/icegatherer.go @@ -409,3 +409,54 @@ func (g *ICEGatherer) collectStats(collector *statsReportCollector) { collector.Done() }(collector, agent) } + +func (g *ICEGatherer) getSelectedCandidatePairStats() (ICECandidatePairStats, bool) { + agent := g.getAgent() + if agent == nil { + return ICECandidatePairStats{}, false + } + + selectedCandidatePairStats, isAvailable := agent.GetSelectedCandidatePairStats() + if !isAvailable { + return ICECandidatePairStats{}, false + } + + state, err := toStatsICECandidatePairState(selectedCandidatePairStats.State) + if err != nil { + g.log.Error(err.Error()) + } + + pairID := newICECandidatePairStatsID(selectedCandidatePairStats.LocalCandidateID, selectedCandidatePairStats.RemoteCandidateID) + return ICECandidatePairStats{ + Timestamp: statsTimestampFrom(selectedCandidatePairStats.Timestamp), + Type: StatsTypeCandidatePair, + ID: pairID, + // TransportID: + LocalCandidateID: selectedCandidatePairStats.LocalCandidateID, + RemoteCandidateID: selectedCandidatePairStats.RemoteCandidateID, + State: state, + Nominated: selectedCandidatePairStats.Nominated, + PacketsSent: selectedCandidatePairStats.PacketsSent, + PacketsReceived: selectedCandidatePairStats.PacketsReceived, + BytesSent: selectedCandidatePairStats.BytesSent, + BytesReceived: selectedCandidatePairStats.BytesReceived, + LastPacketSentTimestamp: statsTimestampFrom(selectedCandidatePairStats.LastPacketSentTimestamp), + LastPacketReceivedTimestamp: statsTimestampFrom(selectedCandidatePairStats.LastPacketReceivedTimestamp), + FirstRequestTimestamp: statsTimestampFrom(selectedCandidatePairStats.FirstRequestTimestamp), + LastRequestTimestamp: statsTimestampFrom(selectedCandidatePairStats.LastRequestTimestamp), + LastResponseTimestamp: statsTimestampFrom(selectedCandidatePairStats.LastResponseTimestamp), + TotalRoundTripTime: selectedCandidatePairStats.TotalRoundTripTime, + CurrentRoundTripTime: selectedCandidatePairStats.CurrentRoundTripTime, + AvailableOutgoingBitrate: selectedCandidatePairStats.AvailableOutgoingBitrate, + AvailableIncomingBitrate: selectedCandidatePairStats.AvailableIncomingBitrate, + CircuitBreakerTriggerCount: selectedCandidatePairStats.CircuitBreakerTriggerCount, + RequestsReceived: selectedCandidatePairStats.RequestsReceived, + RequestsSent: selectedCandidatePairStats.RequestsSent, + ResponsesReceived: selectedCandidatePairStats.ResponsesReceived, + ResponsesSent: selectedCandidatePairStats.ResponsesSent, + RetransmissionsReceived: selectedCandidatePairStats.RetransmissionsReceived, + RetransmissionsSent: selectedCandidatePairStats.RetransmissionsSent, + ConsentRequestsSent: selectedCandidatePairStats.ConsentRequestsSent, + ConsentExpiredTimestamp: statsTimestampFrom(selectedCandidatePairStats.ConsentExpiredTimestamp), + }, true +} diff --git a/icetransport.go b/icetransport.go index e7b8f8ec1c4..a86d4f94f1c 100644 --- a/icetransport.go +++ b/icetransport.go @@ -70,6 +70,12 @@ func (t *ICETransport) GetSelectedCandidatePair() (*ICECandidatePair, error) { return NewICECandidatePair(&local, &remote), nil } +// GetSelectedCandidatePairStats returns the selected candidate pair stats on which packets are sent +// if there is no selected pair empty stats, false is returned to indicate stats not available +func (t *ICETransport) GetSelectedCandidatePairStats() (ICECandidatePairStats, bool) { + return t.gatherer.getSelectedCandidatePairStats() +} + // NewICETransport creates a new NewICETransport. func NewICETransport(gatherer *ICEGatherer, loggerFactory logging.LoggerFactory) *ICETransport { iceTransport := &ICETransport{ diff --git a/icetransport_test.go b/icetransport_test.go index 0d7d55e0427..bcbe0bd2953 100644 --- a/icetransport_test.go +++ b/icetransport_test.go @@ -98,10 +98,14 @@ func TestICETransport_GetSelectedCandidatePair(t *testing.T) { offererSelectedPair, err := offerer.SCTP().Transport().ICETransport().GetSelectedCandidatePair() assert.NoError(t, err) assert.Nil(t, offererSelectedPair) + _, statsAvailable := offerer.SCTP().Transport().ICETransport().GetSelectedCandidatePairStats() + assert.False(t, statsAvailable) answererSelectedPair, err := answerer.SCTP().Transport().ICETransport().GetSelectedCandidatePair() assert.NoError(t, err) assert.Nil(t, answererSelectedPair) + _, statsAvailable = answerer.SCTP().Transport().ICETransport().GetSelectedCandidatePairStats() + assert.False(t, statsAvailable) assert.NoError(t, signalPair(offerer, answerer)) peerConnectionConnected.Wait() @@ -109,10 +113,14 @@ func TestICETransport_GetSelectedCandidatePair(t *testing.T) { offererSelectedPair, err = offerer.SCTP().Transport().ICETransport().GetSelectedCandidatePair() assert.NoError(t, err) assert.NotNil(t, offererSelectedPair) + _, statsAvailable = offerer.SCTP().Transport().ICETransport().GetSelectedCandidatePairStats() + assert.True(t, statsAvailable) answererSelectedPair, err = answerer.SCTP().Transport().ICETransport().GetSelectedCandidatePair() assert.NoError(t, err) assert.NotNil(t, answererSelectedPair) + _, statsAvailable = answerer.SCTP().Transport().ICETransport().GetSelectedCandidatePairStats() + assert.True(t, statsAvailable) closePairNow(t, offerer, answerer) }