@@ -206,6 +206,22 @@ defmodule ExICE.ICEAgent do
206
206
GenServer . cast ( ice_agent , { :send_data , data } )
207
207
end
208
208
209
+ @ doc """
210
+ Gathers ICE agent statistics.
211
+
212
+ * `bytes_sent` - data bytes sent. This does not include connectivity checks and UDP/IP header sizes.
213
+ * `bytes_received` - data bytes received. This does not include connectivity checks and UDP/IP header sizes.
214
+ * `candidate_pairs` - list of current candidate pairs. It will change after doing an ICE restart.
215
+ """
216
+ @ spec get_stats ( pid ( ) ) :: % {
217
+ bytes_sent: non_neg_integer ( ) ,
218
+ bytes_received: non_neg_integer ,
219
+ candidate_pairs: [ CandidatePair . t ( ) ]
220
+ }
221
+ def get_stats ( ice_agent ) do
222
+ GenServer . call ( ice_agent , :get_stats )
223
+ end
224
+
209
225
@ doc """
210
226
Restarts ICE.
211
227
@@ -280,7 +296,10 @@ defmodule ExICE.ICEAgent do
280
296
remote_pwd: nil ,
281
297
remote_cands: [ ] ,
282
298
stun_servers: stun_servers ,
283
- turn_servers: [ ]
299
+ turn_servers: [ ] ,
300
+ # stats
301
+ bytes_sent: 0 ,
302
+ bytes_received: 0
284
303
}
285
304
286
305
{ :ok , state }
@@ -311,6 +330,17 @@ defmodule ExICE.ICEAgent do
311
330
{ :reply , { :ok , state . local_ufrag , state . local_pwd } , state }
312
331
end
313
332
333
+ @ impl true
334
+ def handle_call ( :get_stats , _from , state ) do
335
+ stats = % {
336
+ bytes_sent: state . bytes_sent ,
337
+ bytes_received: state . bytes_received ,
338
+ candidate_pairs: Map . values ( state . checklist )
339
+ }
340
+
341
+ { :reply , stats , state }
342
+ end
343
+
314
344
@ impl true
315
345
def handle_cast (
316
346
{ :set_remote_credentials , ufrag , pwd } ,
@@ -439,8 +469,8 @@ defmodule ExICE.ICEAgent do
439
469
List . first ( state . prev_valid_pairs )
440
470
441
471
dst = { pair . remote_cand . address , pair . remote_cand . port }
442
- do_send ( pair . local_cand . socket , dst , data )
443
- { :noreply , state }
472
+ bytes_sent = do_send ( pair . local_cand . socket , dst , data )
473
+ { :noreply , % { state | bytes_sent: state . bytes_sent + bytes_sent } }
444
474
end
445
475
446
476
@ impl true
@@ -587,7 +617,7 @@ defmodule ExICE.ICEAgent do
587
617
end
588
618
else
589
619
notify ( state . on_data , { :data , packet } )
590
- { :noreply , state }
620
+ { :noreply , % { state | bytes_received: state . bytes_received + byte_size ( packet ) } }
591
621
end
592
622
end
593
623
@@ -1716,17 +1746,19 @@ defmodule ExICE.ICEAgent do
1716
1746
# retrying after getting EPERM seems to help
1717
1747
case :gen_udp . send ( socket , dst , data ) do
1718
1748
:ok ->
1719
- :ok
1749
+ byte_size ( data )
1720
1750
1721
1751
err ->
1722
1752
Logger . error ( "UDP send error: #{ inspect ( err ) } . Retrying..." )
1723
1753
1724
1754
case :gen_udp . send ( socket , dst , data ) do
1725
1755
:ok ->
1726
1756
Logger . debug ( "Successful retry" )
1757
+ byte_size ( data )
1727
1758
1728
1759
err ->
1729
1760
Logger . error ( "Unseccessful retry: #{ inspect ( err ) } . Giving up." )
1761
+ 0
1730
1762
end
1731
1763
end
1732
1764
end
0 commit comments