Skip to content

Commit

Permalink
Document and refactor PeerConnection options (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
mickel8 authored Nov 6, 2023
1 parent 1aeef5b commit d86ac87
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 73 deletions.
4 changes: 3 additions & 1 deletion lib/ex_webrtc/ice_candidate.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
defmodule ExWebRTC.IceCandidate do
@moduledoc false
@moduledoc """
ICE candidate
"""

# not exacly the same as W3 IceCandidate
@type t() :: %__MODULE__{
Expand Down
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 @@ -46,12 +48,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 @@ -103,16 +108,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.ice_servers)
dtls_transport = DTLSTransport.new(ice_agent)

state = %__MODULE__{
Expand Down Expand Up @@ -425,8 +421,6 @@ defmodule ExWebRTC.PeerConnection do
end

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

Expand Down
81 changes: 26 additions & 55 deletions lib/ex_webrtc/peer_connection/configuration.ex
Original file line number Diff line number Diff line change
@@ -1,72 +1,43 @@
defmodule ExWebRTC.PeerConnection.Configuration do
@moduledoc false

@type bundle_policy() ::
:balanced
| :max_compat
| :max_bundle
@moduledoc """
PeerConnection configuration
"""

@type ice_server() :: %{
optional(:credential) => String.t(),
optional(:username) => String.t(),
:urls => [String.t()] | String.t()
}

# TODO implement
@type certificate() :: :TODO

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

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

@type 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,
ice_candidate_pool_size: 0,
ice_servers: [],
ice_transport_policy: :all,
peer_identity: nil,
rtcp_mux_policy: :require
@typedoc """
Options that can be passed to `ExWebRTC.PeerConnection.start_link/1`.
@spec check_support(t()) :: :ok
def check_support(config) do
if config.ice_transport_policy != :all do
raise "#{inspect(config.ice_transport_policy)} ice transport policy is currently not supported"
end
Currently, ExWebRTC always uses the following config:
* bundle_policy - max_bundle
* ice_candidate_pool_size - 0
* ice_transport_policy - all
* rtcp_mux_policy - require
if config.ice_candidate_pool_size != 0 do
raise "Ice candidate pool size different than 0 (pre-gathering) is currently not supported"
end
This config cannot be changed.
"""
@type options() :: [ice_servers: [ice_server()]]

if config.bundle_policy != :max_bundle do
raise "Bundle policy options different than :max_bundle are currently not supported"
end
@typedoc false
@type t() :: %__MODULE__{ice_servers: [ice_server()]}

if config.certificates != nil do
raise "Certificates configuration option is currently not supported"
end
defstruct ice_servers: []

if config.peer_identity != nil do
raise "Identify option is currently not supported"
end
@doc false
@spec from_options!(options()) :: t()
def from_options!(options) do
config = struct!(__MODULE__, options)

if config.rtcp_mux_policy != :require do
raise "RTCP mux policy option :negotiate is currently not supported"
end
# ATM, ExICE does not support relay via TURN
stun_servers =
config.ice_servers
|> Enum.flat_map(&List.wrap(&1.urls))
|> Enum.filter(&String.starts_with?(&1, "stun:"))

:ok
%__MODULE__{config | ice_servers: stun_servers}
end
end
4 changes: 3 additions & 1 deletion lib/ex_webrtc/session_description.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
defmodule ExWebRTC.SessionDescription do
@moduledoc false
@moduledoc """
SessionDescription
"""

@type description_type() ::
:answer
Expand Down

0 comments on commit d86ac87

Please sign in to comment.