Skip to content

Commit

Permalink
Add configuration module
Browse files Browse the repository at this point in the history
  • Loading branch information
LVala committed Sep 22, 2023
1 parent 90c96a7 commit 662a180
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 36 deletions.
68 changes: 39 additions & 29 deletions lib/ex_webrtc/peer_connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,26 @@ defmodule ExWebRTC.PeerConnection do

use GenServer

alias __MODULE__.Configuration
alias ExWebRTC.SessionDescription

@type peer_connection() :: GenServer.server()

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

@type configuration() :: [
bundle_policy: bundle_policy(),
certificates: term(),
ice_candidate_pool_size: term(),
ice_servers: term(),
ice_transport_policy: term(),
peer_identity: term(),
rtcp_mux_policy: term()
]

@type offer_options() :: [ice_restart: boolean()]
@type answer_options() :: []

@enforce_keys [:config]
defstruct @enforce_keys ++
[
:current_local_desc,
:pending_local_desc,
:current_remote_desc,
:pending_remote_desc,
:ice_agent,
transceivers: [],
signaling_state: :stable
]

#### API ####

def start_link(configuration \\ []) do
Expand All @@ -35,22 +33,26 @@ defmodule ExWebRTC.PeerConnection do
GenServer.start(__MODULE__, configuration)
end

@spec create_offer(peer_connection(), offer_options()) :: {:ok, SessionDescription.t()} | {:error, :TODO} # TODO reason
@spec create_offer(peer_connection(), offer_options()) ::
{:ok, SessionDescription.t()} | {:error, :TODO}
def create_offer(peer_connection, options \\ []) do
GenServer.call(peer_connection, {:create_offer, options})
end

@spec create_answer(peer_connection(), answer_options()) :: {:ok, SessionDescription.t()} | {:error, :TODO} # TODO reasons
@spec create_answer(peer_connection(), answer_options()) ::
{:ok, SessionDescription.t()} | {:error, :TODO}
def create_answer(peer_connection, options \\ []) do
GenServer.call(peer_connection, {:create_answer, options})
end

@spec set_local_description(peer_connection(), SessionDescription.t()) :: :ok | {:error, :TODO} # TODO resons
@spec set_local_description(peer_connection(), SessionDescription.t()) ::
:ok | {:error, :TODO}
def set_local_description(peer_connection, description) do
GenServer.call(peer_connection, {:set_local_description, description})
end

@spec set_remote_description(peer_connection(), SessionDescription.t()) :: :ok | {:error, :TODO} # TODO resons
@spec set_remote_description(peer_connection(), SessionDescription.t()) ::
:ok | {:error, :TODO}
def set_remote_description(peer_connection, description) do
GenServer.call(peer_connection, {:set_remote_description, description})
end
Expand All @@ -59,41 +61,49 @@ defmodule ExWebRTC.PeerConnection do

@impl true
def init(config) do
_bundle_policy = Keyword.get(config, :bundle_policy, :balanced)
{:ok, %{}}
config = struct(Configuration, config)
:ok = Configuration.check_support(config)

state = %__MODULE__{config: config}

{:ok, state}
end

@impl true
def handle_call({:create_offer, options}, _from, state) do
_ice_restart = Keyword.get(options, :ice_restart, false)

sdp =
# TODO probably will need to move SDP stuff to its module
sdp =
%{ExSDP.new() | timing: %ExSDP.Timing{start_time: 0, stop_time: 0}}
|> ExSDP.add_attribute({:ice_options, "trickle"})

# identity?

# for each RTPTransceiver add "m=" section
sdp = Enum.reduce(state.transceivers, sdp, &add_media_description/2)

desc = %SessionDescription{type: :offer, sdp: to_string(sdp)}
{:reply, {:ok, desc}, state}
end

@impl true
def handle_call({:create_answer, _options}, _from, state) do
sdp = ExSDP.new()

desc = %SessionDescription{type: :answer, sdp: to_string(sdp)}
{:reply, {:ok, desc}, state}
# TODO
{:reply, :ok, state}
end

@impl true
def handle_call({:set_local_description, _desc}, _from, state) do
# TODO
{:reply, :ok, state}
end

@impl true
def handle_call({:set_remote_description, _desc}, _from, state) do
# TODO
{:reply, :ok, state}
end

defp add_media_description(_transceiver, sdp) do
# TODO
sdp
end
end
64 changes: 64 additions & 0 deletions lib/ex_webrtc/peer_connection/configuration.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
defmodule ExWebRTC.PeerConnection.Configuration do
@moduledoc false

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

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

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

@type t() :: %__MODULE__{
bundle_policy: bundle_policy(),
# TODO certs type
certificates: term(),
ice_candidate_pool_size: non_neg_integer(),
ice_servers: [String.t()],
ice_transport_policy: ice_transport_policy(),
peer_identity: String.t(),
rtcp_mux_policy: rtcp_mux_policy()
}

defstruct bundle_policy: :balanced,
certificates: nil,
ice_candidate_pool_size: 0,
ice_servers: [],
ice_transport_policy: :all,
peer_identity: nil,
rtcp_mux_policy: :require

@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

if config.ice_candidate_pool_size != 0 do
raise "Ice candidate pool size different than 0 (pre-gathering) is currently not supported"
end

if config.bundle_policy != :max_bundle do
raise "Bundle policy options different than :max_bundle are currently not supported"
end

if config.certificates != nil do
raise "Certificates configuration option is currently not supported"
end

if config.peer_identity != nil do
raise "Identify option is currently not supported"
end

if config.rtcp_mux_policy != :require do
raise "RTCP mux policy option :negotiate is currently not supported"
end

:ok
end
end
10 changes: 10 additions & 0 deletions lib/ex_webrtc/peer_connection/signaling_state.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
defmodule ExWebRTC.PeerConnection.SignalingState do
@moduledoc false

@type t() ::
:stable
| :have_local_offer
| :have_remote_offer
| :have_local_pranswer
| :have_remote_pranswer
end
31 changes: 31 additions & 0 deletions lib/ex_webrtc/peer_connection/transceiver.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
defmodule ExWebRTC.PeerConnection.Transceiver do
@moduledoc false

@type kind() ::
:audio
| :video

@type direction() ::
:sendrecv
| :sendonly
| :recvonly
| :inactive
| :stopped

@type options() :: [
direction
]

# @type t() :: %__MODULE__{
# mid: String.t() | nil,
# current_direction: direction() | nil,
# direction: direction(),
# receiver: term(),
# sender: term(),
# send_encodings: term(),
# streams: term()
# }
#
# @enforce_keys [:direction]
# defstruct @enforce_keys ++ [:mid, :current_direction]
end
14 changes: 7 additions & 7 deletions lib/ex_webrtc/session_description.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ defmodule ExWebRTC.SessionDescription do
@moduledoc false

@type description_type() ::
:answer
| :offer
| :pranswer
| :rollback
:answer
| :offer
| :pranswer
| :rollback

@type t() :: %__MODULE__{
type: description_type(),
sdp: String.t()
}
type: description_type(),
sdp: String.t()
}

@enforce_keys [:type, :sdp]
defstruct @enforce_keys
Expand Down

0 comments on commit 662a180

Please sign in to comment.