Skip to content

Commit e5118db

Browse files
committed
Add get_dtls_transport_state
1 parent e662577 commit e5118db

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

lib/ex_webrtc/peer_connection.ex

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ defmodule ExWebRTC.PeerConnection do
5353
@type ice_connection_state() ::
5454
:new | :checking | :connected | :completed | :failed | :disconnected | :closed
5555

56+
@typedoc """
57+
Possible DTLS transport state.
58+
59+
For the exact meaning, refer to the [RTCDtlsTransport: state property](https://developer.mozilla.org/en-US/docs/Web/API/RTCDtlsTransport/state)
60+
"""
61+
@type dtls_transport_state() :: :new | :connecting | :connected | :failed
62+
5663
@typedoc """
5764
Possible signaling states.
5865
@@ -65,6 +72,11 @@ defmodule ExWebRTC.PeerConnection do
6572
6673
Most of the messages match the [RTCPeerConnection events](https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection#events),
6774
except for:
75+
* `:dtls_transport_state_change` - traditional WebRTC implementation does not emit such event.
76+
Instead, developer can read DTLS transport state by iterating over RTP receiver/senders, and checking their
77+
DTLS transports states. See https://developer.mozilla.org/en-US/docs/Web/API/RTCRtpSender/transport.
78+
However, because Elixir WebRTC creates a single DTLS transport for all receivers and senders, there is one generic
79+
notification informing about DTLS transport state.
6880
* `:track_muted`, `:track_ended` - these match the [MediaStreamTrack events](https://developer.mozilla.org/en-US/docs/Web/API/MediaStreamTrack#events).
6981
* `:data` - data received from DataChannel identified by its `ref`.
7082
* `:rtp` and `:rtcp` - these contain packets received by the PeerConnection. The third element of `:rtp` tuple is a simulcast RID and is set to `nil` if simulcast
@@ -79,6 +91,7 @@ defmodule ExWebRTC.PeerConnection do
7991
| {:ice_candidate, ICECandidate.t()}
8092
| {:ice_connection_state_change, ice_connection_state()}
8193
| {:ice_gathering_state_change, ice_gathering_state()}
94+
| {:dtls_transport_state_change, dtls_transport_state()}
8295
| :negotiation_needed
8396
| {:signaling_state_change, signaling_state()}
8497
| {:data_channel_state_change, DataChannel.ref(), DataChannel.ready_state()}
@@ -292,6 +305,16 @@ defmodule ExWebRTC.PeerConnection do
292305
GenServer.call(peer_connection, :get_ice_gathering_state)
293306
end
294307

308+
@doc """
309+
Returns the DTLS transport state.
310+
311+
For more information, refer to the [RTCDtlsTransport: state property](https://developer.mozilla.org/en-US/docs/Web/API/RTCDtlsTransport/state).
312+
"""
313+
@spec get_dtls_transport_state(peer_connection()) :: dtls_transport_state()
314+
def get_dtls_transport_state(peer_connection) do
315+
GenServer.call(peer_connection, :get_dtls_transport_state)
316+
end
317+
295318
@doc """
296319
Returns the signaling state.
297320
@@ -675,6 +698,11 @@ defmodule ExWebRTC.PeerConnection do
675698
{:reply, state.ice_gathering_state, state}
676699
end
677700

701+
@impl true
702+
def handle_call(:get_dtls_transport_state, _from, state) do
703+
{:reply, state.dtls_state, state}
704+
end
705+
678706
@impl true
679707
def handle_call(:get_signaling_state, _from, state) do
680708
{:reply, state.signaling_state, state}
@@ -1359,9 +1387,9 @@ defmodule ExWebRTC.PeerConnection do
13591387

13601388
@impl true
13611389
def handle_info({:dtls_transport, _pid, {:state_change, new_dtls_state}}, state) do
1362-
next_conn_state = next_conn_state(state.ice_state, new_dtls_state)
1390+
notify(state.owner, {:dtls_transport_state_change, new_dtls_state})
13631391

1364-
notify(state.owner, {:dtls_state_change, new_dtls_state})
1392+
next_conn_state = next_conn_state(state.ice_state, new_dtls_state)
13651393

13661394
state =
13671395
%{state | dtls_state: new_dtls_state}

test/ex_webrtc/peer_connection_test.exs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,11 @@ defmodule ExWebRTC.PeerConnectionTest do
298298
:ok = PeerConnection.close(pc2)
299299
end
300300

301+
test "get_dtls_transport_state/1" do
302+
{:ok, pc} = PeerConnection.start_link()
303+
assert PeerConnection.get_dtls_transport_state(pc) == :new
304+
end
305+
301306
describe "get_local_description/1" do
302307
test "includes ICE candidates" do
303308
{:ok, pc} = PeerConnection.start()

0 commit comments

Comments
 (0)