forked from inlivedev/sfu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clientstats.go
250 lines (194 loc) · 5.38 KB
/
clientstats.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package sfu
import (
"context"
"errors"
"sync"
"time"
"github.com/pion/interceptor/pkg/stats"
)
var (
ErrCLientStatsNotFound = errors.New("client stats not found")
)
type staticVoiceActivityStats struct {
start uint32
duration uint32
}
type voiceActivityStats struct {
mu sync.Mutex
active bool
staticVoiceActivityStats
}
type TrackStats struct {
senders map[string]stats.Stats
senderBitrates map[string]uint32
receivers map[string]stats.Stats
receiverBitrates map[string]uint32
}
type ClientStats struct {
mu sync.Mutex
senderMu sync.RWMutex
receiverMu sync.RWMutex
Client *Client
*TrackStats
voiceActivity voiceActivityStats
}
func newClientStats(c *Client) *ClientStats {
cstats := &ClientStats{
senderMu: sync.RWMutex{},
receiverMu: sync.RWMutex{},
Client: c,
TrackStats: &TrackStats{
senders: make(map[string]stats.Stats),
receivers: make(map[string]stats.Stats),
senderBitrates: make(map[string]uint32),
receiverBitrates: make(map[string]uint32),
},
voiceActivity: voiceActivityStats{
mu: sync.Mutex{},
active: false,
},
}
go cstats.monitorBitrates(c.Context())
return cstats
}
func (c *ClientStats) monitorBitrates(ctx context.Context) {
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
var lastSenderBytesSent = make(map[string]uint64)
var lastReceiverBytesReceived = make(map[string]uint64)
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
lastSenderBytesSent = c.updateSenderBitrates(lastSenderBytesSent)
lastReceiverBytesReceived = c.updateReceiverBitrates(lastReceiverBytesReceived)
}
}
}
func (c *ClientStats) updateSenderBitrates(lastSenderBytesSent map[string]uint64) map[string]uint64 {
c.senderMu.Lock()
defer c.senderMu.Unlock()
for id, stats := range c.senders {
lastBytesSent, ok := lastSenderBytesSent[id]
if !ok {
lastSenderBytesSent[id] = stats.OutboundRTPStreamStats.BytesSent
continue
}
delta := stats.OutboundRTPStreamStats.BytesSent - lastBytesSent
lastSenderBytesSent[id] = stats.OutboundRTPStreamStats.BytesSent
c.senderBitrates[id] = uint32(delta) * 8
}
return lastSenderBytesSent
}
func (c *ClientStats) updateReceiverBitrates(lastReceiverBytesSent map[string]uint64) map[string]uint64 {
c.receiverMu.Lock()
defer c.receiverMu.Unlock()
for id, stats := range c.receivers {
lastBytesReceived, ok := lastReceiverBytesSent[id]
if !ok {
lastReceiverBytesSent[id] = stats.InboundRTPStreamStats.BytesReceived
continue
}
delta := stats.InboundRTPStreamStats.BytesReceived - lastBytesReceived
lastReceiverBytesSent[id] = stats.InboundRTPStreamStats.BytesReceived
c.receiverBitrates[id] = uint32(delta) * 8
}
return lastReceiverBytesSent
}
func (c *ClientStats) removeSenderStats(trackId string) {
c.senderMu.Lock()
defer c.senderMu.Unlock()
delete(c.senders, trackId)
}
func (c *ClientStats) removeReceiverStats(trackId string) {
c.receiverMu.Lock()
defer c.receiverMu.Unlock()
delete(c.receivers, trackId)
}
func (c *ClientStats) Senders() map[string]stats.Stats {
c.senderMu.RLock()
defer c.senderMu.RUnlock()
stats := make(map[string]stats.Stats)
for k, v := range c.senders {
stats[k] = v
}
return stats
}
func (c *ClientStats) GetSender(id string) (stats.Stats, error) {
c.senderMu.RLock()
defer c.senderMu.RUnlock()
sender, ok := c.senders[id]
if !ok {
return stats.Stats{}, ErrCLientStatsNotFound
}
return sender, nil
}
func (c *ClientStats) GetSenderBitrate(id string) (uint32, error) {
c.senderMu.RLock()
defer c.senderMu.RUnlock()
bitrate, ok := c.senderBitrates[id]
if !ok {
return 0, ErrCLientStatsNotFound
}
return bitrate, nil
}
func (c *ClientStats) SetSender(id string, stats stats.Stats) {
c.senderMu.Lock()
defer c.senderMu.Unlock()
c.senders[id] = stats
}
func (c *ClientStats) Receivers() map[string]stats.Stats {
c.receiverMu.RLock()
defer c.receiverMu.RUnlock()
stats := make(map[string]stats.Stats)
for k, v := range c.receivers {
stats[k] = v
}
return stats
}
func (c *ClientStats) GetReceiverBitrate(id, rid string) (uint32, error) {
c.receiverMu.RLock()
defer c.receiverMu.RUnlock()
bitrate, ok := c.receiverBitrates[id+rid]
if !ok {
return 0, ErrCLientStatsNotFound
}
return bitrate, nil
}
func (c *ClientStats) GetReceiver(id, rid string) (stats.Stats, error) {
c.receiverMu.RLock()
defer c.receiverMu.RUnlock()
receiver, ok := c.receivers[id+rid]
if !ok {
return stats.Stats{}, ErrCLientStatsNotFound
}
return receiver, nil
}
func (c *ClientStats) SetReceiver(id, rid string, stats stats.Stats) {
c.receiverMu.Lock()
defer c.receiverMu.Unlock()
idrid := id + rid
c.receivers[idrid] = stats
}
// UpdateVoiceActivity updates voice activity duration
// 0 timestamp means ended
func (c *ClientStats) UpdateVoiceActivity(ts uint32, clockRate uint32) {
c.mu.Lock()
defer c.mu.Unlock()
if !c.voiceActivity.active && ts != 0 {
c.voiceActivity.active = true
c.voiceActivity.start = ts
} else if c.voiceActivity.active && ts != 0 {
duration := (ts - c.voiceActivity.start) * 1000 / clockRate
c.voiceActivity.start = ts
c.voiceActivity.duration = c.voiceActivity.duration + duration
} else if c.voiceActivity.active && ts == 0 {
c.voiceActivity.active = false
}
}
func (c *ClientStats) VoiceActivity() time.Duration {
c.mu.Lock()
defer c.mu.Unlock()
return time.Duration(c.voiceActivity.duration) * time.Millisecond
}