Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate signaling #12

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/membrane_webrtc/simple_websocket_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ defmodule Membrane.WebRTC.SimpleWebSocketServer do
@doc false
@spec child_spec({options, SignalingChannel.t()}) :: Supervisor.child_spec()
def child_spec({opts, signaling}) do
opts = opts |> Keyword.validate!([:port, ip: {127, 0, 0, 1}]) |> Map.new()
opts = opts |> validate_options!() |> Map.new()

Supervisor.child_spec(
{Bandit,
Expand All @@ -37,6 +37,9 @@ defmodule Membrane.WebRTC.SimpleWebSocketServer do
)
end

@spec validate_options!(options()) :: options() | no_return()
def validate_options!(options), do: Keyword.validate!(options, [:port, ip: {127, 0, 0, 1}])

@doc false
@spec start_link_supervised(pid, options) :: SignalingChannel.t()
def start_link_supervised(utility_supervisor, opts) do
Expand Down
2 changes: 2 additions & 0 deletions lib/membrane_webrtc/sink.ex
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ defmodule Membrane.WebRTC.Sink do

@impl true
def handle_init(_ctx, opts) do
:ok = Membrane.WebRTC.Utils.validate_signaling!(opts.signaling)

spec =
child(:webrtc, %ExWebRTCSink{
signaling: opts.signaling,
Expand Down
2 changes: 2 additions & 0 deletions lib/membrane_webrtc/source.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ defmodule Membrane.WebRTC.Source do
def handle_init(_ctx, opts) do
{signaling, opts} = opts |> Map.from_struct() |> Map.pop!(:signaling)

:ok = Membrane.WebRTC.Utils.validate_signaling!(signaling)

spec =
child(:webrtc, %ExWebRTCSource{
signaling: signaling,
Expand Down
18 changes: 18 additions & 0 deletions lib/membrane_webrtc/utils.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule Membrane.WebRTC.Utils do
@moduledoc false

alias Membrane.WebRTC.{SignalingChannel, SimpleWebSocketServer}

@spec validate_signaling!(SignalingChannel.t() | {:websocket, SimpleWebSocketServer.options()}) ::
:ok | no_return()
def validate_signaling!(%SignalingChannel{}), do: :ok

def validate_signaling!({:websocket, options}) do
_options = SimpleWebSocketServer.validate_options!(options)
:ok
end

def validate_signaling!(signaling) do
raise "Invalid signaling: #{inspect(signaling, pretty: true)}"
end
end
Loading