Skip to content

Commit

Permalink
Improve examples/stats
Browse files Browse the repository at this point in the history
Add PeerConnection.GetStats() call with a Type Switch
  • Loading branch information
Sean-Der committed Aug 26, 2024
1 parent 661a92f commit 076fcc7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
9 changes: 7 additions & 2 deletions examples/stats/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
32 changes: 29 additions & 3 deletions examples/stats/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ import (
"io"
"os"
"strings"
"sync/atomic"
"time"

"github.com/pion/interceptor"
"github.com/pion/interceptor/pkg/stats"
"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 ❤️.
Expand Down Expand Up @@ -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)
}
}()

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 076fcc7

Please sign in to comment.