mirror of
https://github.com/pion/webrtc.git
synced 2025-09-26 19:21:12 +08:00
Improve examples/stats
Add PeerConnection.GetStats() call with a Type Switch
This commit is contained in:
@@ -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
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user