forked from inlivedev/sfu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroom_stats.go
62 lines (53 loc) · 1.43 KB
/
room_stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package sfu
import (
"time"
"github.com/pion/webrtc/v3"
)
type StatTracks struct {
Audio int `json:"audio"`
Video int `json:"video"`
}
type RoomStats struct {
ActiveSessions int `json:"active_sessions"`
Clients int `json:"clients"`
PacketLost int64 `json:"packet_lost"`
ByteSent uint64 `json:"bytes_sent"`
BytesReceived uint64 `json:"bytes_received"`
Tracks StatTracks `json:"tracks"`
Timestamp time.Time `json:"timestamp"`
}
func generateCurrentStats(r *Room) RoomStats {
var (
bytesReceived uint64
bytesSent uint64
packet_lost int64
)
clients := r.GetSFU().GetClients()
for _, c := range clients {
stats := c.GetStats()
for _, stat := range stats.Receiver {
bytesReceived += stat.InboundRTPStreamStats.BytesReceived
packet_lost += stat.InboundRTPStreamStats.PacketsLost
}
for _, stat := range stats.Sender {
bytesSent += stat.OutboundRTPStreamStats.BytesSent
}
}
return RoomStats{
ActiveSessions: calculateActiveSessions(r.GetSFU().GetClients()),
Clients: len(r.GetSFU().GetClients()),
PacketLost: packet_lost,
BytesReceived: bytesReceived,
ByteSent: bytesSent,
Timestamp: time.Now(),
}
}
func calculateActiveSessions(clients map[string]*Client) int {
count := 0
for _, c := range clients {
if c.GetPeerConnection().ConnectionState() == webrtc.PeerConnectionStateConnected {
count++
}
}
return count
}