Skip to content

Commit

Permalink
fix listen ip conversion (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
satoren authored Jan 23, 2024
1 parent b89b868 commit 69ec8df
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 34 deletions.
11 changes: 2 additions & 9 deletions lib/mediasoup.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,8 @@ defmodule Mediasoup do
@typedoc "https://mediasoup.org/documentation/v3/mediasoup/sctp-parameters/#NumSctpStreams"
@type num_sctp_streams :: %{OS: integer(), MIS: integer()}
@typedoc "https://mediasoup.org/documentation/v3/mediasoup/api/#TransportListenIp"
@type transport_listen_ip :: %{:ip => String.t(), optional(:announcedIp) => String.t() | nil}
@type transport_listen_ip :: Mediasoup.TransportListenInfo.listen_ip()

@typedoc "https://mediasoup.org/documentation/v3/mediasoup/api/#TransportListenInfo"
@type transport_listen_info :: %{
:ip => String.t(),
:protocol => :tcp | :udp,
optional(:announcedIp) => String.t() | nil,
optional(:port) => integer(),
optional(:sendBufferSize) => integer(),
optional(:recvBufferSize) => integer()
}
@type transport_listen_info :: Mediasoup.TransportListenInfo.t()
end
21 changes: 14 additions & 7 deletions lib/pipe_transport.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@ defmodule Mediasoup.PipeTransport do
@moduledoc """
https://mediasoup.org/documentation/v3/mediasoup/api/#PipeTransport
"""
alias Mediasoup.{PipeTransport, Consumer, DataConsumer, Producer, DataProducer, NifWrap, Nif}
alias Mediasoup.{
TransportListenInfo,
PipeTransport,
Consumer,
DataConsumer,
Producer,
DataProducer,
NifWrap,
Nif
}

require NifWrap
use GenServer, restart: :temporary

Expand Down Expand Up @@ -41,16 +51,13 @@ defmodule Mediasoup.PipeTransport do
}
def normalize(%Options{listen_ip: listen_ip, port: port} = option)
when not is_nil(listen_ip) do
listen_info = TransportListenInfo.create(listen_ip, "udp", port)

normalize(%Options{
option
| listen_ip: nil,
port: nil,
listen_info: %{
protocol: "udp",
ip: listen_ip.ip,
announcedIp: listen_ip[:announcedIp],
port: port
}
listen_info: listen_info
})
end

Expand Down
13 changes: 5 additions & 8 deletions lib/plain_transport.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Mediasoup.PlainTransport do
https://mediasoup.org/documentation/v3/mediasoup/api/#PlainTransport
"""

alias Mediasoup.{PlainTransport, Consumer, Producer, NifWrap, Nif}
alias Mediasoup.{TransportListenInfo, PlainTransport, Consumer, Producer, NifWrap, Nif}
require NifWrap
use GenServer, restart: :temporary

Expand All @@ -29,7 +29,7 @@ defmodule Mediasoup.PlainTransport do
enable_srtp: nil

@type t :: %Options{
listen_info: Mediasoup.transport_listen_info() | nil,
listen_info: TransportListenInfo.t() | nil,
# deprecated use listen_info instead
listen_ip: Mediasoup.transport_listen_ip() | nil,
# deprecated use listen_info instead
Expand Down Expand Up @@ -62,16 +62,13 @@ defmodule Mediasoup.PlainTransport do

def normalize(%Options{listen_ip: listen_ip, port: port} = option)
when not is_nil(listen_ip) do
listen_info = TransportListenInfo.create(listen_ip, "udp", port)

normalize(%Options{
option
| listen_ip: nil,
port: nil,
listen_info: %{
protocol: "udp",
ip: listen_ip.ip,
announcedIp: listen_ip[:announcedIp],
port: port
}
listen_info: listen_info
})
end

Expand Down
47 changes: 47 additions & 0 deletions lib/transport_listen_info.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
defmodule Mediasoup.TransportListenInfo do
@moduledoc """
https://mediasoup.org/documentation/v3/mediasoup/api/#TransportListenInfo
"""

@typedoc "https://mediasoup.org/documentation/v3/mediasoup/api/#TransportListenIp"
@type listen_ip :: %{:ip => String.t(), optional(:announcedIp) => String.t() | nil}

@typedoc "https://mediasoup.org/documentation/v3/mediasoup/api/#TransportListenInfo"
@type t :: %{
:ip => String.t(),
:protocol => :tcp | :udp,
optional(:announcedIp) => String.t() | nil,
optional(:port) => integer(),
optional(:sendBufferSize) => integer(),
optional(:recvBufferSize) => integer()
}

@enforce_keys [:ip, :protocol]

defstruct [:ip, :protocol, :announcedIp, :port, :sendBufferSize, :recvBufferSize]

def normalize_listen_ip(ip) when is_binary(ip) do
%{:ip => ip}
end

def normalize_listen_ip(%{ip: _} = ip) do
ip
end

def create(ip, protocol) do
listen_ip = normalize_listen_ip(ip)
struct(__MODULE__, Map.merge(listen_ip, %{:protocol => protocol}))
end

def create(ip, protocol, port) do
listen_ip = normalize_listen_ip(ip)

struct(
__MODULE__,
Map.merge(listen_ip, %{
:protocol => protocol,
:port => port
})
)
end
end
21 changes: 12 additions & 9 deletions lib/webrtc_transport.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@ defmodule Mediasoup.WebRtcTransport do
https://mediasoup.org/documentation/v3/mediasoup/api/#WebRtcTransport
"""

alias Mediasoup.{WebRtcTransport, Consumer, Producer, NifWrap, Nif, DataConsumer, DataProducer}
alias Mediasoup.{
TransportListenInfo,
WebRtcTransport,
Consumer,
Producer,
NifWrap,
Nif,
DataConsumer,
DataProducer
}

require NifWrap
use GenServer, restart: :temporary

Expand Down Expand Up @@ -112,14 +122,7 @@ defmodule Mediasoup.WebRtcTransport do
} = option
)
when not is_nil(listen_ips) do
listen_ips =
Enum.map(listen_ips, fn listen_ip ->
if is_binary(listen_ip) do
%{ip: listen_ip}
else
listen_ip
end
end)
listen_ips = Enum.map(listen_ips, &TransportListenInfo.normalize_listen_ip/1)

# Convert deprecated TransportListenIps to TransportListenInfos.
protocols = protocols(option)
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule MediasoupElixir.MixProject do
use Mix.Project

@version "0.9.0"
@version "0.9.1"
@repo "https://github.com/oviceinc/mediasoup-elixir"
@description """
Elixir wrapper for mediasoup
Expand Down
16 changes: 16 additions & 0 deletions test/transport_listen_info_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule Mediasoup.TransportListenInfoTest do
use ExUnit.Case

alias Mediasoup.TransportListenInfo

test "create" do
assert %Mediasoup.TransportListenInfo{announcedIp: "1.1.1.1", ip: "127.0.0.1", protocol: :udp} ==
TransportListenInfo.create(
%{ip: "127.0.0.1", announcedIp: "1.1.1.1"},
:udp
)

assert %Mediasoup.TransportListenInfo{ip: "127.0.0.1", protocol: :tcp} ==
TransportListenInfo.create("127.0.0.1", :tcp)
end
end

0 comments on commit 69ec8df

Please sign in to comment.