Skip to content

Commit 3eb812f

Browse files
committed
Add logger_metadata option to PeerConnection
1 parent 20277d1 commit 3eb812f

File tree

6 files changed

+50
-12
lines changed

6 files changed

+50
-12
lines changed

lib/ex_webrtc/dtls_transport.ex

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,33 @@ defmodule ExWebRTC.DTLSTransport do
4646
base64_certificate: binary()
4747
}
4848

49-
@spec start_link(ICETransport.t(), pid()) :: GenServer.on_start()
50-
def start_link(ice_transport \\ DefaultICETransport, ice_pid) do
49+
@typedoc """
50+
DTLS Transport configuration options
51+
52+
* `ice_transport` - the module implementing the `ExICE.ICETransport` behavior.
53+
* `ice_pid` - the PID of the ICE transport process which the DTLSTransport interacts with.
54+
* `logger_metadata` - a keyword list of metadata to be attached to the Logger for all logs emitted by the DTLSTransport process.
55+
"""
56+
@type opts() :: [
57+
ice_transport: ICETransport.t(),
58+
ice_pid: pid(),
59+
logger_metadata: Enumerable.t({atom(), term()})
60+
]
61+
62+
@spec start_link(opts()) :: GenServer.on_start()
63+
def start_link(opts) do
64+
ice_transport = opts[:ice_transport] || DefaultICETransport
65+
logger_metadata = opts[:logger_metadata] || []
66+
67+
ice_pid = Keyword.fetch!(opts, :ice_pid)
68+
5169
behaviour = ice_transport.__info__(:attributes)[:behaviour] || []
5270

5371
unless ICETransport in behaviour do
5472
raise "DTLSTransport requires ice_transport to implement ExWebRTC.ICETransport behaviour."
5573
end
5674

57-
GenServer.start_link(__MODULE__, [ice_transport, ice_pid, self()])
75+
GenServer.start_link(__MODULE__, [ice_transport, ice_pid, self(), logger_metadata])
5876
end
5977

6078
@spec set_ice_connected(dtls_transport()) :: :ok
@@ -117,7 +135,9 @@ defmodule ExWebRTC.DTLSTransport do
117135
end
118136

119137
@impl true
120-
def init([ice_transport, ice_pid, owner]) do
138+
def init([ice_transport, ice_pid, owner, logger_metadata]) do
139+
Logger.metadata(logger_metadata)
140+
121141
{pkey, cert} = ExDTLS.generate_key_cert()
122142
fingerprint = ExDTLS.get_cert_fingerprint(cert)
123143

lib/ex_webrtc/peer_connection.ex

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,8 @@ defmodule ExWebRTC.PeerConnection do
605605

606606
@impl true
607607
def handle_continue(config, _state) do
608+
Logger.metadata(config.logger_metadata)
609+
608610
{:ok, _} = Registry.register(ExWebRTC.Registry, self(), self())
609611

610612
ice_config = [
@@ -614,11 +616,20 @@ defmodule ExWebRTC.PeerConnection do
614616
aggressive_nomination: config.ice_aggressive_nomination,
615617
host_to_srflx_ip_mapper: config.host_to_srflx_ip_mapper,
616618
ports: config.ice_port_range,
617-
on_data: nil
619+
on_data: nil,
620+
logger_metadata: config.logger_metadata
618621
]
619622

620623
{:ok, ice_pid} = DefaultICETransport.start_link(ice_config)
621-
{:ok, dtls_transport} = DTLSTransport.start_link(DefaultICETransport, ice_pid)
624+
625+
dtls_config = [
626+
ice_transport: DefaultICETransport,
627+
ice_pid: ice_pid,
628+
logger_metadata: config.logger_metadata
629+
]
630+
631+
{:ok, dtls_transport} = DTLSTransport.start_link(dtls_config)
632+
622633
# route data to the DTLSTransport
623634
:ok = DefaultICETransport.on_data(ice_pid, dtls_transport)
624635

lib/ex_webrtc/peer_connection/configuration.ex

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ defmodule ExWebRTC.PeerConnection.Configuration do
158158
* `ice_ip_filter` - filter applied when gathering local candidates. By default, all IP addresses are accepted.
159159
* `ice_port_range` - range of ports that ICE will use for gathering host candidates. Defaults to ephemeral ports.
160160
* `ice_aggressive_nomination` - whether ICE agent should use aggressive nomination. By default, ICE agent
161+
* `logger_metadata` - a keyword list of metadata to be attached to the Logger for all logs emitted by the PeerConnection and its child processes.
161162
relies on the regular nomination defined in RFC 8445. However, some WebRTC implementations require
162163
the controlling side to nominate a pair before they can start sending data (e.g. Pion, Firefox).
163164
This can result in longer, connection establishment time as regular nomination nominates only one pair,
@@ -205,7 +206,8 @@ defmodule ExWebRTC.PeerConnection.Configuration do
205206
video_codecs: [RTPCodecParameters.t()] | [video_codec_name()],
206207
features: [feature()],
207208
rtp_header_extensions: [rtp_header_extension()],
208-
rtcp_feedbacks: [rtcp_feedback()]
209+
rtcp_feedbacks: [rtcp_feedback()],
210+
logger_metadata: Enumerable.t({atom(), term()})
209211
]
210212

211213
@typedoc """
@@ -226,7 +228,8 @@ defmodule ExWebRTC.PeerConnection.Configuration do
226228
video_codecs: [RTPCodecParameters.t()],
227229
audio_extensions: [Extmap.t()],
228230
video_extensions: [Extmap.t()],
229-
features: [feature()]
231+
features: [feature()],
232+
logger_metadata: Enumerable.t({atom(), term()})
230233
}
231234

232235
@enforce_keys [
@@ -244,7 +247,8 @@ defmodule ExWebRTC.PeerConnection.Configuration do
244247
host_to_srflx_ip_mapper: nil,
245248
audio_codecs: @default_audio_codecs,
246249
video_codecs: @default_video_codecs,
247-
features: @default_features
250+
features: @default_features,
251+
logger_metadata: []
248252
]
249253

250254
@doc """

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ defmodule ExWebRTC.MixProject do
5757
defp deps do
5858
[
5959
{:ex_sdp, "~> 1.0"},
60-
{:ex_ice, github: "elixir-webrtc/ex_ice"},
60+
{:ex_ice, github: "elixir-webrtc/ex_ice", branch: "logger-metadata"},
6161
{:ex_dtls, "~> 0.18.0"},
6262
{:ex_libsrtp, "~> 0.7.1"},
6363
{:ex_rtp, "~> 0.4.0"},

mix.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"erlex": {:hex, :erlex, "0.2.7", "810e8725f96ab74d17aac676e748627a07bc87eb950d2b83acd29dc047a30595", [:mix], [], "hexpm", "3ed95f79d1a844c3f6bf0cea61e0d5612a42ce56da9c03f01df538685365efb0"},
1313
"ex_doc": {:hex, :ex_doc, "0.38.2", "504d25eef296b4dec3b8e33e810bc8b5344d565998cd83914ffe1b8503737c02", [:mix], [{:earmark_parser, "~> 1.4.44", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.0", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14 or ~> 1.0", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1 or ~> 1.0", [hex: :makeup_erlang, repo: "hexpm", optional: false]}, {:makeup_html, ">= 0.1.0", [hex: :makeup_html, repo: "hexpm", optional: true]}], "hexpm", "732f2d972e42c116a70802f9898c51b54916e542cc50968ac6980512ec90f42b"},
1414
"ex_dtls": {:hex, :ex_dtls, "0.18.0", "0815e3384bb0c1e6c06559012479cf9a94a501ddf46c3df54dc2d1b169e29d5c", [:mix], [{:bundlex, "~> 1.5.3", [hex: :bundlex, repo: "hexpm", optional: false]}, {:unifex, "~> 1.0", [hex: :unifex, repo: "hexpm", optional: false]}], "hexpm", "562eda1815eeaed8360b2b5c34d4db5b453794bc096404a4c64f193fa7b18bf2"},
15-
"ex_ice": {:git, "https://github.com/elixir-webrtc/ex_ice.git", "a4f930b19a1e92c5bbaf64e24e0035942f6ff280", []},
15+
"ex_ice": {:git, "https://github.com/elixir-webrtc/ex_ice.git", "5776db9cf06da6b664539df1522f5d136957484c", [branch: "logger-metadata"]},
1616
"ex_libsrtp": {:hex, :ex_libsrtp, "0.7.2", "211bd89c08026943ce71f3e2c0231795b99cee748808ed3ae7b97cd8d2450b6b", [:mix], [{:bunch, "~> 1.6", [hex: :bunch, repo: "hexpm", optional: false]}, {:bundlex, "~> 1.3", [hex: :bundlex, repo: "hexpm", optional: false]}, {:membrane_precompiled_dependency_provider, "~> 0.1.0", [hex: :membrane_precompiled_dependency_provider, repo: "hexpm", optional: false]}, {:unifex, "~> 1.1", [hex: :unifex, repo: "hexpm", optional: false]}], "hexpm", "2e20645d0d739a4ecdcf8d4810a0c198120c8a2f617f2b75b2e2e704d59f492a"},
1717
"ex_rtcp": {:hex, :ex_rtcp, "0.4.0", "f9e515462a9581798ff6413583a25174cfd2101c94a2ebee871cca7639886f0a", [:mix], [], "hexpm", "28956602cf210d692fcdaf3f60ca49681634e1deb28ace41246aee61ee22dc3b"},
1818
"ex_rtp": {:hex, :ex_rtp, "0.4.0", "1f1b5c1440a904706011e3afbb41741f5da309ce251cb986690ce9fd82636658", [:mix], [], "hexpm", "0f72d80d5953a62057270040f0f1ee6f955c08eeae82ac659c038001d7d5a790"},

test/ex_webrtc/dtls_transport_test.exs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@ defmodule ExWebRTC.DTLSTransportTest do
118118

119119
setup do
120120
{:ok, ice_pid} = MockICETransport.start_link(tester: self())
121-
assert {:ok, dtls} = DTLSTransport.start_link(MockICETransport, ice_pid)
121+
122+
assert {:ok, dtls} =
123+
DTLSTransport.start_link(ice_transport: MockICETransport, ice_pid: ice_pid)
124+
122125
MockICETransport.on_data(ice_pid, dtls)
123126
assert_receive {:dtls_transport, ^dtls, {:state_change, :new}}
124127

0 commit comments

Comments
 (0)