-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
135 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,41 +3,129 @@ defmodule ExWebRTC.PeerConnection.Configuration do | |
PeerConnection configuration | ||
""" | ||
|
||
@default_codecs [:opus, :h264, :vp8] | ||
|
||
@codecs %{ | ||
Check warning on line 8 in lib/ex_webrtc/peer_connection/configuration.ex GitHub Actions / Test (OTP 26 / Elixir 1.15)
Check warning on line 8 in lib/ex_webrtc/peer_connection/configuration.ex GitHub Actions / Lint (OTP 26 / Elixir 1.15)
|
||
:audio => [%{name: :opus, clock_rate: 48_000, channels: 2, payload_type: 111}], | ||
:video => [ | ||
%{name: :h264, clock_rate: 90_000, channels: nil, payload_type: 98}, | ||
%{name: :vp8, clock_rate: 90_000, channels: nil, payload_type: 96} | ||
] | ||
} | ||
|
||
@rtp_hdr_extensions %{ | ||
:mid => "urn:ietf:params:rtp-hdrext:sdes:mid", | ||
:audio_level => "urn:ietf:params:rtp-hdrext:ssrc-audio-level" | ||
} | ||
|
||
@mandatory_rtp_hdr_exts [:mid] | ||
|
||
@type ice_server() :: %{ | ||
optional(:credential) => String.t(), | ||
optional(:username) => String.t(), | ||
:urls => [String.t()] | String.t() | ||
} | ||
|
||
@type codec() :: :opus | :h264 | :vp8 | ||
|
||
@type rtp_hdr_extension() :: :audio_level | ||
|
||
@typedoc """ | ||
Options that can be passed to `ExWebRTC.PeerConnection.start_link/1`. | ||
Currently, ExWebRTC always uses the following config: | ||
* `ice_servers` - list of STUN servers to use. | ||
TURN servers are not supported right now and will be filtered out. | ||
* `codecs` - list of codecs to use. | ||
Use `default_codecs/0` to get a list of default codecs. | ||
This option overrides default codecs. | ||
If you wish to add codecs to default ones do | ||
`codecs: Configuration.get_default_codecs() ++ my_codecs` | ||
* `rtp_hdr_extensions` - list of RTP header extensions to use. | ||
MID extension is enabled by default and cannot be turned off. | ||
Besides options listed above, ExWebRTC uses the following config: | ||
* bundle_policy - max_bundle | ||
* ice_candidate_pool_size - 0 | ||
* ice_transport_policy - all | ||
* rtcp_mux_policy - require | ||
This config cannot be changed. | ||
""" | ||
@type options() :: [ice_servers: [ice_server()]] | ||
@type options() :: [ | ||
ice_servers: [ice_server()], | ||
codecs: [codec()], | ||
rtp_hdr_extensions: [rtp_hdr_extension()] | ||
] | ||
|
||
@typedoc false | ||
@type t() :: %__MODULE__{ice_servers: [ice_server()]} | ||
@type t() :: %__MODULE__{ | ||
ice_servers: [ice_server()], | ||
codecs: [codec()], | ||
rtp_hdr_extensions: [rtp_hdr_extension()] | ||
} | ||
|
||
defstruct ice_servers: [], | ||
codecs: @default_codecs, | ||
rtp_hdr_extensions: @mandatory_rtp_hdr_exts | ||
|
||
defstruct ice_servers: [] | ||
@doc """ | ||
Returns a list of codecs that are used by default. | ||
""" | ||
@spec default_codecs() :: [codec()] | ||
def default_codecs(), do: @default_codecs | ||
|
||
@doc false | ||
@spec from_options!(options()) :: t() | ||
def from_options!(options) do | ||
config = struct!(__MODULE__, options) | ||
options = | ||
options | ||
|> add_mandatory_rtp_hdr_extensions() | ||
|> resolve_rtp_hdr_extensions() | ||
|> resolve_codecs() | ||
# ATM, ExICE does not support relay via TURN | ||
|> reject_turn_servers() | ||
|
||
struct!(__MODULE__, options) | ||
end | ||
|
||
@doc false | ||
@spec is_supported_codec(t(), ExSDP.Attribute.RTPMapping.t()) :: boolean() | ||
def is_supported_codec(config, rtp_mapping) do | ||
supported_codecs = Enum.map(config.codecs, fn codec -> "#{codec}" end) | ||
rtp_mapping.encoding in supported_codecs | ||
end | ||
|
||
@doc false | ||
@spec is_supported_rtp_hdr_extension(t(), ExSDP.Attribute.Extmap.t()) :: boolean() | ||
def is_supported_rtp_hdr_extension(config, rtp_hdr_extension) do | ||
rtp_hdr_extension.uri in config.rtp_hdr_extensions | ||
end | ||
|
||
@doc false | ||
@spec is_supported_rtcp_feedback(t(), ExSDP.Attribute.RTCPFeedback.t()) :: boolean() | ||
def is_supported_rtcp_feedback(_config, _rtcp_feedback), do: false | ||
|
||
defp add_mandatory_rtp_hdr_extensions(options) do | ||
Keyword.update(options, :rtp_hdr_extensions, @mandatory_rtp_hdr_exts, fn exts -> | ||
exts ++ @mandatory_rtp_hdr_exts | ||
end) | ||
end | ||
|
||
defp resolve_rtp_hdr_extensions(options) do | ||
rtp_hdr_extensions = | ||
Enum.map(options[:rtp_hdr_extensions], fn ext -> Map.fetch!(@rtp_hdr_extensions, ext) end) | ||
|
||
Keyword.put(options, :rtp_hdr_extensions, rtp_hdr_extensions) | ||
end | ||
|
||
# ATM, ExICE does not support relay via TURN | ||
stun_servers = | ||
config.ice_servers | ||
defp resolve_codecs(options) do | ||
Check warning on line 121 in lib/ex_webrtc/peer_connection/configuration.ex GitHub Actions / Test (OTP 26 / Elixir 1.15)
|
||
end | ||
|
||
defp reject_turn_servers(options) do | ||
Keyword.update(options, :ice_servers, [], fn ice_servers -> | ||
ice_servers | ||
|> Enum.flat_map(&List.wrap(&1.urls)) | ||
|> Enum.filter(&String.starts_with?(&1, "stun:")) | ||
|
||
%__MODULE__{config | ice_servers: stun_servers} | ||
end) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters