From 076fcc799620fc11b3959c2e8345e0ca555d23e4 Mon Sep 17 00:00:00 2001 From: Sean DuBois Date: Mon, 26 Aug 2024 16:10:24 -0400 Subject: [PATCH] Improve examples/stats Add PeerConnection.GetStats() call with a Type Switch --- examples/stats/README.md | 9 +++++++-- examples/stats/main.go | 32 +++++++++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/examples/stats/README.md b/examples/stats/README.md index 3bcea016756..eef5af74be5 100644 --- a/examples/stats/README.md +++ b/examples/stats/README.md @@ -27,8 +27,8 @@ Run `echo $BROWSER_SDP | stats` Copy the text that `stats` just emitted and copy into second text area ### Hit 'Start Session' in jsfiddle -The `stats` program will now print the InboundRTPStreamStats for each incoming stream. You will see the following in -your console. The exact fields will change as we add more values. +The `stats` program will now print the InboundRTPStreamStats for each incoming stream and Remote IP+Ports. +You will see the following in your console. The exact fields will change as we add more values. ``` Stats for: video/VP8 @@ -42,6 +42,11 @@ InboundRTPStreamStats: FIRCount: 0 PLICount: 0 NACKCount: 0 + + +remote-candidate IP(192.168.1.93) Port(59239) +remote-candidate IP(172.18.176.1) Port(59241) +remote-candidate IP(fd4d:d991:c340:6749:8c53:ee52:ae8c:14d4) Port(59238) ``` Congrats, you have used Pion WebRTC! Now start building something cool diff --git a/examples/stats/main.go b/examples/stats/main.go index 1cf7082eba3..7e6c6e2d420 100644 --- a/examples/stats/main.go +++ b/examples/stats/main.go @@ -16,6 +16,7 @@ import ( "io" "os" "strings" + "sync/atomic" "time" "github.com/pion/interceptor" @@ -23,6 +24,9 @@ import ( "github.com/pion/webrtc/v4" ) +// How ofter to print WebRTC stats +const statsInterval = time.Second * 5 + // nolint:gocognit func main() { // Everything below is the Pion WebRTC API! Thanks for using it ❤️. @@ -87,13 +91,14 @@ func main() { fmt.Printf("New incoming track with codec: %s\n", track.Codec().MimeType) go func() { + // Print the stats for this individual track for { stats := statsGetter.Get(uint32(track.SSRC())) fmt.Printf("Stats for: %s\n", track.Codec().MimeType) fmt.Println(stats.InboundRTPStreamStats) - time.Sleep(time.Second * 5) + time.Sleep(statsInterval) } }() @@ -106,10 +111,14 @@ func main() { } }) + var iceConnectionState atomic.Value + iceConnectionState.Store(webrtc.ICEConnectionStateNew) + // Set the handler for ICE connection state // This will notify you when the peer has connected/disconnected peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) { fmt.Printf("Connection State has changed %s \n", connectionState.String()) + iceConnectionState.Store(connectionState) }) // Wait for the offer to be pasted @@ -145,8 +154,25 @@ func main() { // Output the answer in base64 so we can paste it in browser fmt.Println(encode(peerConnection.LocalDescription())) - // Block forever - select {} + for { + time.Sleep(statsInterval) + + // Stats are only printed after completed to make Copy/Pasting easier + if iceConnectionState.Load() == webrtc.ICEConnectionStateChecking { + continue + } + + // Only print the remote IPs seen + for _, s := range peerConnection.GetStats() { + switch stat := s.(type) { + case webrtc.ICECandidateStats: + if stat.Type == webrtc.StatsTypeRemoteCandidate { + fmt.Printf("%s IP(%s) Port(%d)\n", stat.Type, stat.IP, stat.Port) + } + default: + } + } + } } // Read from stdin until we get a newline