Skip to content

Commit a092fda

Browse files
committed
Add get_stats
1 parent 7f85ac4 commit a092fda

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

lib/ex_webrtc/app.ex

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
defmodule ExWebRTC.App do
2+
use Application
3+
4+
def start(_type, _args) do
5+
IO.inspect(:loading_ex_webrtc_app)
6+
children = [{Registry, keys: :unique, name: ExWebRTC.Registry}]
7+
Supervisor.start_link(children, strategy: :one_for_one)
8+
end
9+
end
10+

lib/ex_webrtc/ice_transport.ex

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ defmodule ExWebRTC.ICETransport do
1414
@callback restart(pid()) :: :ok
1515
@callback send_data(pid(), binary()) :: :ok
1616
@callback set_remote_credentials(pid(), ufrag :: binary(), pwd :: binary()) :: :ok
17+
@callback get_stats(pid()) :: map()
1718
@callback stop(pid()) :: :ok
1819
end
1920

@@ -43,5 +44,7 @@ defmodule ExWebRTC.DefaultICETransport do
4344
@impl true
4445
defdelegate set_remote_credentials(pid, ufrag, pwd), to: ICEAgent
4546
@impl true
47+
defdelegate get_stats(pid), to: ICEAgent
48+
@impl true
4649
defdelegate stop(pid), to: ICEAgent
4750
end

lib/ex_webrtc/peer_connection.ex

+29
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ defmodule ExWebRTC.PeerConnection do
5959
@type connection_state() :: :closed | :failed | :disconnected | :new | :connecting | :connected
6060

6161
#### 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+
6267
@spec start_link(Configuration.options()) :: GenServer.on_start()
6368
def start_link(options \\ []) do
6469
configuration = Configuration.from_options!(options)
@@ -157,6 +162,11 @@ defmodule ExWebRTC.PeerConnection do
157162
GenServer.call(peer_connection, {:remove_track, sender_id})
158163
end
159164

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+
160170
@spec send_rtp(peer_connection(), String.t(), ExRTP.Packet.t()) :: :ok
161171
def send_rtp(peer_connection, track_id, packet) do
162172
GenServer.cast(peer_connection, {:send_rtp, track_id, packet})
@@ -171,6 +181,7 @@ defmodule ExWebRTC.PeerConnection do
171181

172182
@impl true
173183
def init({owner, config}) do
184+
{:ok, _} = Registry.register(ExWebRTC.Registry, self(), self())
174185
ice_config = [stun_servers: config.ice_servers, ip_filter: config.ice_ip_filter, on_data: nil]
175186
{:ok, ice_pid} = DefaultICETransport.start_link(:controlled, ice_config)
176187
{:ok, dtls_transport} = DTLSTransport.start_link(DefaultICETransport, ice_pid)
@@ -182,6 +193,7 @@ defmodule ExWebRTC.PeerConnection do
182193

183194
state = %{
184195
owner: owner,
196+
stats_id: Utils.generate_id(),
185197
config: config,
186198
current_local_desc: nil,
187199
pending_local_desc: nil,
@@ -558,6 +570,23 @@ defmodule ExWebRTC.PeerConnection do
558570
end
559571
end
560572

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+
561590
@impl true
562591
def handle_cast({:send_rtp, track_id, packet}, state) do
563592
# TODO: iterating over transceivers is not optimal

mix.exs

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ defmodule ExWebRTC.MixProject do
3333

3434
def application do
3535
[
36+
mod: {ExWebRTC.App, []},
3637
extra_applications: [:logger]
3738
]
3839
end

0 commit comments

Comments
 (0)