@@ -33,7 +33,18 @@ type CandidatePair struct {
33
33
// stats
34
34
currentRoundTripTime int64 // in ns
35
35
totalRoundTripTime int64 // in ns
36
- responsesReceived uint64
36
+
37
+ requestsReceived uint64
38
+ requestsSent uint64
39
+ responsesReceived uint64
40
+ responsesSent uint64
41
+
42
+ firstRequestSentAt atomic.Value // time.Time
43
+ lastRequestSentAt atomic.Value // time.Time
44
+ firstReponseReceivedAt atomic.Value // time.Time
45
+ lastResponseReceivedAt atomic.Value // time.Time
46
+ firstRequestReceivedAt atomic.Value // time.Time
47
+ lastRequestReceivedAt atomic.Value // time.Time
37
48
}
38
49
39
50
func (p * CandidatePair ) String () string {
@@ -127,6 +138,10 @@ func (p *CandidatePair) UpdateRoundTripTime(rtt time.Duration) {
127
138
atomic .StoreInt64 (& p .currentRoundTripTime , rttNs )
128
139
atomic .AddInt64 (& p .totalRoundTripTime , rttNs )
129
140
atomic .AddUint64 (& p .responsesReceived , 1 )
141
+
142
+ now := time .Now ()
143
+ p .firstReponseReceivedAt .CompareAndSwap (nil , now )
144
+ p .lastResponseReceivedAt .Store (now )
130
145
}
131
146
132
147
// CurrentRoundTripTime returns the current round trip time in seconds
@@ -141,8 +156,101 @@ func (p *CandidatePair) TotalRoundTripTime() float64 {
141
156
return time .Duration (atomic .LoadInt64 (& p .totalRoundTripTime )).Seconds ()
142
157
}
143
158
159
+ // RequestsReceived returns the total number of connectivity checks received
160
+ // https://www.w3.org/TR/webrtc-stats/#dom-rtcicecandidatepairstats-requestsreceived
161
+ func (p * CandidatePair ) RequestsReceived () uint64 {
162
+ return atomic .LoadUint64 (& p .requestsReceived )
163
+ }
164
+
165
+ // RequestsSent returns the total number of connectivity checks sent
166
+ // https://www.w3.org/TR/webrtc-stats/#dom-rtcicecandidatepairstats-requestssent
167
+ func (p * CandidatePair ) RequestsSent () uint64 {
168
+ return atomic .LoadUint64 (& p .requestsSent )
169
+ }
170
+
144
171
// ResponsesReceived returns the total number of connectivity responses received
145
172
// https://www.w3.org/TR/webrtc-stats/#dom-rtcicecandidatepairstats-responsesreceived
146
173
func (p * CandidatePair ) ResponsesReceived () uint64 {
147
174
return atomic .LoadUint64 (& p .responsesReceived )
148
175
}
176
+
177
+ // ResponsesSent returns the total number of connectivity responses sent
178
+ // https://www.w3.org/TR/webrtc-stats/#dom-rtcicecandidatepairstats-responsessent
179
+ func (p * CandidatePair ) ResponsesSent () uint64 {
180
+ return atomic .LoadUint64 (& p .responsesSent )
181
+ }
182
+
183
+ // FirstRequestSentAt returns the timestamp of the first connectivity check sent.
184
+ func (p * CandidatePair ) FirstRequestSentAt () time.Time {
185
+ if v , ok := p .firstRequestSentAt .Load ().(time.Time ); ok {
186
+ return v
187
+ }
188
+
189
+ return time.Time {}
190
+ }
191
+
192
+ // LastRequestSentAt returns the timestamp of the last connectivity check sent.
193
+ func (p * CandidatePair ) LastRequestSentAt () time.Time {
194
+ if v , ok := p .lastRequestSentAt .Load ().(time.Time ); ok {
195
+ return v
196
+ }
197
+
198
+ return time.Time {}
199
+ }
200
+
201
+ // FirstReponseReceivedAt returns the timestamp of the first connectivity response received.
202
+ func (p * CandidatePair ) FirstReponseReceivedAt () time.Time {
203
+ if v , ok := p .firstReponseReceivedAt .Load ().(time.Time ); ok {
204
+ return v
205
+ }
206
+
207
+ return time.Time {}
208
+ }
209
+
210
+ // LastResponseReceivedAt returns the timestamp of the last connectivity response received.
211
+ func (p * CandidatePair ) LastResponseReceivedAt () time.Time {
212
+ if v , ok := p .lastResponseReceivedAt .Load ().(time.Time ); ok {
213
+ return v
214
+ }
215
+
216
+ return time.Time {}
217
+ }
218
+
219
+ // FirstRequestReceivedAt returns the timestamp of the first connectivity check received.
220
+ func (p * CandidatePair ) FirstRequestReceivedAt () time.Time {
221
+ if v , ok := p .firstRequestReceivedAt .Load ().(time.Time ); ok {
222
+ return v
223
+ }
224
+
225
+ return time.Time {}
226
+ }
227
+
228
+ // LastRequestReceivedAt returns the timestamp of the last connectivity check received.
229
+ func (p * CandidatePair ) LastRequestReceivedAt () time.Time {
230
+ if v , ok := p .lastRequestReceivedAt .Load ().(time.Time ); ok {
231
+ return v
232
+ }
233
+
234
+ return time.Time {}
235
+ }
236
+
237
+ // UpdateRequestSent increments the number of requests sent and updates the timestamp.
238
+ func (p * CandidatePair ) UpdateRequestSent () {
239
+ atomic .AddUint64 (& p .requestsSent , 1 )
240
+ now := time .Now ()
241
+ p .firstRequestSentAt .CompareAndSwap (nil , now )
242
+ p .lastRequestSentAt .Store (now )
243
+ }
244
+
245
+ // UpdateResponseSent increments the number of responses sent.
246
+ func (p * CandidatePair ) UpdateResponseSent () {
247
+ atomic .AddUint64 (& p .responsesSent , 1 )
248
+ }
249
+
250
+ // UpdateRequestReceived increments the number of requests received and updates the timestamp.
251
+ func (p * CandidatePair ) UpdateRequestReceived () {
252
+ atomic .AddUint64 (& p .requestsReceived , 1 )
253
+ now := time .Now ()
254
+ p .firstRequestReceivedAt .CompareAndSwap (nil , now )
255
+ p .lastRequestReceivedAt .Store (now )
256
+ }
0 commit comments