Skip to content

Commit

Permalink
Document and refactor PeerConnection options
Browse files Browse the repository at this point in the history
  • Loading branch information
mickel8 committed Nov 3, 2023
1 parent 8f3fac2 commit cfb0d25
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 31 deletions.
26 changes: 10 additions & 16 deletions lib/ex_webrtc/peer_connection.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
defmodule ExWebRTC.PeerConnection do
@moduledoc false
@moduledoc """
PeerConnection
"""

use GenServer

Expand Down Expand Up @@ -47,12 +49,15 @@ defmodule ExWebRTC.PeerConnection do
]

#### API ####

def start_link(configuration \\ []) do
@spec start_link(Configuration.options()) :: GenServer.on_start()
def start_link(options \\ []) do
configuration = Configuration.from_options!(options)
GenServer.start_link(__MODULE__, {self(), configuration})
end

def start(configuration \\ []) do
@spec start(Configuration.options()) :: GenServer.on_start()
def start(options \\ []) do
configuration = Configuration.from_options!(options)
GenServer.start(__MODULE__, {self(), configuration})
end

Expand Down Expand Up @@ -104,16 +109,7 @@ defmodule ExWebRTC.PeerConnection do

@impl true
def init({owner, config}) do
config = struct(Configuration, config)
:ok = Configuration.check_support(config)

# ATM, ExICE does not support relay via TURN
stun_servers =
config.ice_servers
|> Enum.flat_map(&if(is_list(&1.urls), do: &1.urls, else: [&1.urls]))
|> Enum.filter(&String.starts_with?(&1, "stun:"))

{:ok, ice_agent} = ICEAgent.start_link(:controlled, stun_servers: stun_servers)
{:ok, ice_agent} = ICEAgent.start_link(:controlled, stun_servers: config.stun_servers)
{:ok, dtls_client} = ExDTLS.start_link(client_mode: false, dtls_srtp: true)

state = %__MODULE__{
Expand Down Expand Up @@ -438,8 +434,6 @@ defmodule ExWebRTC.PeerConnection do
state = set_description(:remote, type, sdp, state)

{:ok, %{state | transceivers: new_transceivers}}
else
error -> error
end
end

Expand Down
55 changes: 40 additions & 15 deletions lib/ex_webrtc/peer_connection/configuration.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
defmodule ExWebRTC.PeerConnection.Configuration do
@moduledoc false
@moduledoc """
PeerConnection configuration
"""

@type bundle_policy() ::
:balanced
| :max_compat
| :max_bundle
@type bundle_policy() :: :balanced | :max_compat | :max_bundle

@type ice_server() :: %{
optional(:credential) => String.t(),
Expand All @@ -15,23 +14,32 @@ defmodule ExWebRTC.PeerConnection.Configuration do
# TODO implement
@type certificate() :: :TODO

@type ice_transport_policy() ::
:all
| :relay
@type ice_transport_policy() :: :all | :relay

@type rtcp_mux_policy() ::
:negotiate
| :require
@type rtcp_mux_policy() :: :negotiate | :require

@type t() :: %__MODULE__{
@typedoc """
Options that can be passed to `ExWebRTC.PeerConnection.start_link/1`.
"""
@type options() :: [
bundle_policy: bundle_policy(),
certificates: [certificate()],
ice_candidate_pool_size: non_neg_integer(),
ice_servers: [ice_server()],
ice_transport_policy: ice_transport_policy(),
peer_identity: String.t(),
rtcp_mux_policy: rtcp_mux_policy()
}
]

@typep t() :: %__MODULE__{
bundle_policy: bundle_policy(),
certificates: [certificate()],
ice_candidate_pool_size: non_neg_integer(),
ice_servers: [ice_server()],
ice_transport_policy: ice_transport_policy(),
peer_identity: String.t(),
rtcp_mux_policy: rtcp_mux_policy()
}

defstruct bundle_policy: :max_bundle,
certificates: nil,
Expand All @@ -41,8 +49,25 @@ defmodule ExWebRTC.PeerConnection.Configuration do
peer_identity: nil,
rtcp_mux_policy: :require

@spec check_support(t()) :: :ok
def check_support(config) do
@doc false
@spec from_options!(options()) :: t()
def from_options!(options) do
config = struct(__MODULE__, options)

:ok = validate!(config)

# ATM, ExICE does not support relay via TURN
stun_servers =
config.ice_servers
|> Enum.flat_map(&if(is_list(&1.urls), do: &1.urls, else: [&1.urls]))
|> Enum.filter(&String.starts_with?(&1, "stun:"))

%__MODULE__{config | ice_servers: stun_servers}
end

@doc false
@spec validate!(t()) :: :ok
def validate!(config) do
if config.ice_transport_policy != :all do
raise "#{inspect(config.ice_transport_policy)} ice transport policy is currently not supported"
end
Expand Down

0 comments on commit cfb0d25

Please sign in to comment.