@@ -59,6 +59,11 @@ defmodule ExWebRTC.PeerConnection do
59
59
@ type connection_state ( ) :: :closed | :failed | :disconnected | :new | :connecting | :connected
60
60
61
61
#### API ####
62
+ @ spec get_all_peer_connections ( ) :: [ pid ( ) ]
63
+ def get_all_peer_connections ( ) do
64
+ Registry . select ( ExWebRTC.Registry , [ { { :_ , :"$1" , :_ } , [ ] , [ :"$1" ] } ] )
65
+ end
66
+
62
67
@ spec start_link ( Configuration . options ( ) ) :: GenServer . on_start ( )
63
68
def start_link ( options \\ [ ] ) do
64
69
configuration = Configuration . from_options! ( options )
@@ -157,6 +162,11 @@ defmodule ExWebRTC.PeerConnection do
157
162
GenServer . call ( peer_connection , { :remove_track , sender_id } )
158
163
end
159
164
165
+ @ spec get_stats ( peer_connection ( ) ) :: % { String . t ( ) => term ( ) }
166
+ def get_stats ( peer_connection ) do
167
+ GenServer . call ( peer_connection , :get_stats )
168
+ end
169
+
160
170
@ spec send_rtp ( peer_connection ( ) , String . t ( ) , ExRTP.Packet . t ( ) ) :: :ok
161
171
def send_rtp ( peer_connection , track_id , packet ) do
162
172
GenServer . cast ( peer_connection , { :send_rtp , track_id , packet } )
@@ -171,6 +181,7 @@ defmodule ExWebRTC.PeerConnection do
171
181
172
182
@ impl true
173
183
def init ( { owner , config } ) do
184
+ { :ok , _ } = Registry . register ( ExWebRTC.Registry , self ( ) , self ( ) )
174
185
ice_config = [ stun_servers: config . ice_servers , ip_filter: config . ice_ip_filter , on_data: nil ]
175
186
{ :ok , ice_pid } = DefaultICETransport . start_link ( :controlled , ice_config )
176
187
{ :ok , dtls_transport } = DTLSTransport . start_link ( DefaultICETransport , ice_pid )
@@ -182,6 +193,7 @@ defmodule ExWebRTC.PeerConnection do
182
193
183
194
state = % {
184
195
owner: owner ,
196
+ stats_id: Utils . generate_id ( ) ,
185
197
config: config ,
186
198
current_local_desc: nil ,
187
199
pending_local_desc: nil ,
@@ -558,6 +570,23 @@ defmodule ExWebRTC.PeerConnection do
558
570
end
559
571
end
560
572
573
+ @ impl true
574
+ def handle_call ( :get_stats , _from , state ) do
575
+ timestamp = System . os_time ( :millisecond )
576
+
577
+ stats = % {
578
+ state . stats_id => % {
579
+ id: state . stats_id ,
580
+ type: :peer_connection ,
581
+ timestamp: timestamp ,
582
+ datachannels_opened: 0 ,
583
+ datachannels_closed: 0
584
+ }
585
+ }
586
+
587
+ { :reply , stats , state }
588
+ end
589
+
561
590
@ impl true
562
591
def handle_cast ( { :send_rtp , track_id , packet } , state ) do
563
592
# TODO: iterating over transceivers is not optimal
0 commit comments