-
Notifications
You must be signed in to change notification settings - Fork 0
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
Add support for TURN servers #7
Conversation
0c633a2
to
52f2884
Compare
lib/ex_libnice.ex
Outdated
def set_relay_info( | ||
pid, | ||
stream_id, | ||
component_id, | ||
server_ip, | ||
server_port, | ||
username, | ||
password, | ||
relay_type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- server-specific parameters could be bundled together, reducing the number of arguments and allowing to pass multiple servers at once
- allowing enum and
:all
atom for component_id would come in handy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added ability to specify list of component ids. :all
is harder because we don't store information about how many components are created.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't insist, but storing that shouldn't be hard
lib/ex_libnice.ex
Outdated
@@ -66,6 +68,46 @@ defmodule ExLibnice do | |||
GenServer.cast(pid, {:remove_stream, stream_id}) | |||
end | |||
|
|||
@doc """ | |||
Sets TURN server. Can be called multiple times for the same component. `relay_type` can be "udp", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can mention that servers are not overridden
c_src/ex_libnice/native.spec.exs
Outdated
@@ -14,6 +14,15 @@ spec add_stream(state, n_components :: unsigned, name :: string) :: {:ok :: labe | |||
|
|||
spec remove_stream(state, stream_id :: unsigned) :: {:ok :: label} | |||
|
|||
spec set_relay_info(state, stream_id :: unsigned, component_id :: unsigned, server_ip :: string, | |||
server_port :: unsigned, username :: string, password :: string, relay_type :: string) :: | |||
{:ok :: label, state} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this need to return state?
c_src/ex_libnice/native.spec.exs
Outdated
| {:error :: label, :bad_relay_type :: label} | ||
| {:error :: label, :failed_to_set_turn :: label} | ||
|
||
spec forget_relays(state, stream_id :: unsigned, component_id :: unsigned) :: {:ok :: label, state} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as above
0324c76
to
def48cf
Compare
c_src/ex_libnice/native.spec.exs
Outdated
@@ -16,11 +16,11 @@ spec remove_stream(state, stream_id :: unsigned) :: {:ok :: label} | |||
|
|||
spec set_relay_info(state, stream_id :: unsigned, component_id :: unsigned, server_ip :: string, | |||
server_port :: unsigned, username :: string, password :: string, relay_type :: string) :: | |||
{:ok :: label, state} | |||
{:ok :: label} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not return bare :ok :: label
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:ok :: label
results in syntax error but :ok
is ok 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe wrap it in braces (:ok :: label)
c_src/ex_libnice/native.spec.exs
Outdated
| {:error :: label, :bad_relay_type :: label} | ||
| {:error :: label, :failed_to_set_turn :: label} | ||
|
||
spec forget_relays(state, stream_id :: unsigned, component_id :: unsigned) :: {:ok :: label, state} | ||
spec forget_relays(state, stream_id :: unsigned, component_id :: unsigned) :: {:ok :: label} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as above
lib/ex_libnice.ex
Outdated
@type relay_info :: | ||
{server_ip :: String.t(), server_port :: integer(), username :: String.t(), | ||
password :: String.t(), relay_type :: :udp | :tcp | :tls} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd define that as a map or struct, but leave it as is if you consider that an overkill
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would simplify some function clauses but on the other hand imo it's easier for user to specify tuple than writing map with all keys
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's true, but map/struct has important profits for the user also, for example, it's impossible to mistake the order of arguments, or you can provide some defaults, like the default port
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True that, I have fixed it. Now it will also be similar to WebRTC JS API
lib/ex_libnice.ex
Outdated
{:error, _cause} = error -> | ||
Logger.warn(""" | ||
Couldn't set TURN server #{inspect(server_ip)} #{inspect(server_port)} for component: | ||
#{inspect(component_id)} in stream: #{inspect(stream_id)} | ||
""") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd log the cause
too
lib/ex_libnice.ex
Outdated
defp do_set_relay_info( | ||
_state, | ||
stream_id, | ||
component_id, | ||
{server_ip, server_port, _username, _password, _relay_type} | ||
) do | ||
Logger.warn(""" | ||
Couldn't set TURN server #{inspect(server_ip)} #{inspect(server_port)} for component: \ | ||
#{inspect(component_id)} in stream: #{inspect(stream_id)}, reason: bad_relay_type | ||
""") | ||
|
||
{:error, :bad_relay_type} | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's move that clause up and add a guard when relay_type not in [:udp, :tcp, :tls]
. This way we won't accidentally fall into this clause and return a misleading error in the future. I'd log what was passed as the relay type also.
@@ -255,7 +255,7 @@ defmodule ExLibnice do | |||
case Unifex.CNode.call(cnode, :add_stream, [n_components, name]) do | |||
{:ok, stream_id} -> | |||
Logger.debug("New stream_id: #{stream_id}") | |||
{:reply, {:ok, stream_id}, %{state | n_components: n_components}} | |||
{:reply, {:ok, stream_id}, put_in(state.stream_components[stream_id], n_components)} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't we remove that in remove_stream
?
lib/ex_libnice.ex
Outdated
nil -> | ||
Logger.warn(""" | ||
Couldn't set TURN servers. No components for stream id #{inspect(stream_id)}" | ||
""") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't that mean that there's no such stream?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually yes
test/ex_libnice_test.exs
Outdated
%{ | ||
:server_ip => {127, 0, 0, 1}, | ||
:server_port => 3478, | ||
:username => "username", | ||
:password => "password", | ||
:relay_type => :udp | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can use %{key: value}
syntax for maps with atom keys
closes #2