Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve examples/stats #2890

Merged
merged 1 commit into from
Aug 26, 2024
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
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
Loading