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

Add support for TURN servers #7

Merged
merged 11 commits into from
Mar 11, 2021
Merged

Add support for TURN servers #7

merged 11 commits into from
Mar 11, 2021

Conversation

mickel8
Copy link
Member

@mickel8 mickel8 commented Mar 2, 2021

closes #2

@mickel8 mickel8 added enhancement New feature or request webrtc labels Mar 2, 2021
@mickel8 mickel8 self-assigned this Mar 2, 2021
@mickel8 mickel8 force-pushed the turn-servers branch 3 times, most recently from 0c633a2 to 52f2884 Compare March 4, 2021 11:39
@mickel8 mickel8 requested a review from mat-hek March 4, 2021 11:56
@mickel8 mickel8 changed the title Add support for turn servers Add support for TURN servers Mar 4, 2021
Comment on lines 85 to 93
def set_relay_info(
pid,
stream_id,
component_id,
server_ip,
server_port,
username,
password,
relay_type
Copy link
Member

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

Copy link
Member Author

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.

Copy link
Member

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

@@ -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",
Copy link
Member

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

@@ -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}
Copy link
Member

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?

| {: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}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as above

@mickel8 mickel8 force-pushed the turn-servers branch 2 times, most recently from 0324c76 to def48cf Compare March 8, 2021 18:06
@@ -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}
Copy link
Member

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?

Copy link
Member Author

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 👍

Copy link
Member

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)

| {: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}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as above

Comment on lines 27 to 29
@type relay_info ::
{server_ip :: String.t(), server_port :: integer(), username :: String.t(),
password :: String.t(), relay_type :: :udp | :tcp | :tls}
Copy link
Member

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

Copy link
Member Author

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

Copy link
Member

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

Copy link
Member Author

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

Comment on lines 527 to 531
{: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)}
""")
Copy link
Member

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

Comment on lines 537 to 549
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
Copy link
Member

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.

@mickel8 mickel8 requested a review from mat-hek March 9, 2021 17:53
@mat-hek mat-hek removed their request for review March 10, 2021 09:15
@mickel8 mickel8 requested a review from mat-hek March 10, 2021 09:22
@@ -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)}
Copy link
Member

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?

Comment on lines 512 to 515
nil ->
Logger.warn("""
Couldn't set TURN servers. No components for stream id #{inspect(stream_id)}"
""")
Copy link
Member

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually yes

@mickel8 mickel8 requested a review from mat-hek March 10, 2021 14:26
Comment on lines 40 to 46
%{
:server_ip => {127, 0, 0, 1},
:server_port => 3478,
:username => "username",
:password => "password",
:relay_type => :udp
}
Copy link
Member

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

@mickel8 mickel8 merged commit 4fc4687 into master Mar 11, 2021
@mickel8 mickel8 deleted the turn-servers branch March 11, 2021 11:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request webrtc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support TURN servers
2 participants