From 1a8a3058b3c33c16c27a97a985089b81713a92b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20=C5=9Aled=C5=BA?= Date: Thu, 23 Nov 2023 20:15:54 +0100 Subject: [PATCH] wip --- lib/ex_webrtc/peer_connection.ex | 61 +++++++++++++++++++++++++++++--- mix.lock | 2 +- 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/lib/ex_webrtc/peer_connection.ex b/lib/ex_webrtc/peer_connection.ex index 9c0f127c..b4723c43 100644 --- a/lib/ex_webrtc/peer_connection.ex +++ b/lib/ex_webrtc/peer_connection.ex @@ -31,6 +31,20 @@ defmodule ExWebRTC.PeerConnection do streams: [:TODO] ] + @typedoc """ + Messages sent by the ExWebRTC. + """ + @type signal() :: {:ex_webrtc, pid(), connection_state_change()} + + @type connection_state_change() :: {:connectionstatechange, connection_state()} + + @typedoc """ + Possible PeerConnection states. + + For the exact meaning, refer to the [WebRTC W3C, section 4.3.3](https://www.w3.org/TR/webrtc/#rtcpeerconnectionstate-enum) + """ + @type connection_state() :: :closed | :failed | :disconnected | :new | :connecting | :connected + @enforce_keys [:config, :owner] defstruct @enforce_keys ++ [ @@ -39,11 +53,13 @@ defmodule ExWebRTC.PeerConnection do :current_remote_desc, :pending_remote_desc, :ice_agent, - :ice_state, :dtls_transport, demuxer: %Demuxer{}, transceivers: [], + ice_state: nil, + dtls_state: nil, signaling_state: :stable, + conn_state: :new, last_offer: nil, last_answer: nil ] @@ -122,6 +138,8 @@ defmodule ExWebRTC.PeerConnection do dtls_transport: dtls_transport } + notify(state.owner, {:connectionstatechange, :new}) + {:ok, state} end @@ -327,8 +345,11 @@ defmodule ExWebRTC.PeerConnection do end @impl true - def handle_info({:ex_ice, _from, :connected}, state) do - {:noreply, %__MODULE__{state | ice_state: :connected}} + def handle_info({:ex_ice, _from, msg}, state) + when msg in [:checking, :connected, :completed, :failed] do + next_conn_state = next_conn_state(msg, state.dtls_state) + state = update_conn_state(state, next_conn_state) + {:noreply, %__MODULE__{state | ice_state: msg}} end @impl true @@ -346,7 +367,14 @@ defmodule ExWebRTC.PeerConnection do end @impl true - def handle_info({:rtp_data, data}, state) do + def handle_info({:dtls_transport, _pid, {:statechange, new_dtls_state}}, state) do + next_conn_state = next_conn_state(state.ice_state, new_dtls_state) + state = update_conn_state(state, next_conn_state) + {:noreply, state} + end + + @impl true + def handle_info({:dtls_transport, _pid, {:rtp_data, data}}, state) do case Demuxer.demux(state.demuxer, data) do {:ok, demuxer, mid, packet} -> notify(state.owner, {:data, {mid, packet}}) @@ -514,6 +542,31 @@ defmodule ExWebRTC.PeerConnection do defp maybe_next_state(:have_remote_pranswer, :remote, :answer), do: {:ok, :stable} defp maybe_next_state(:have_remote_pranswer, _, _), do: {:error, :invalid_transition} + # TODO support :disconnected state - our ICE doesn't provide disconnected state for now + # TODO support :closed state + defp next_conn_state(ice_state, dtls_state) + + defp next_conn_state(ice_state, dtls_state) + when ice_state in [:new, :checking] and dtls_state in [:new, :connecting], + do: :connecting + + defp next_conn_state(ice_state, dtls_state) + when ice_state in [:connected, :completed] and dtls_state in [:connected], + do: :connected + + defp next_conn_state(ice_state, dtls_state) when ice_state == :failed or dtls_state == :failed, + do: :failed + + defp next_conn_state(ice_state, dtls_state) when ice_state == :new and dtls_state == :new, + do: :new + + defp update_conn_state(%{conn_state: conn_state} = state, conn_state), do: state + + defp update_conn_state(state, new_conn_state) do + notify(state.owner, {:connectionstatechange, new_conn_state}) + %{state | conn_state: new_conn_state} + end + defp set_description(:local, :answer, sdp, state) do # NOTICE: internaly, we don't create SessionDescription # as it would require serialization of sdp diff --git a/mix.lock b/mix.lock index 12c45908..8e749370 100644 --- a/mix.lock +++ b/mix.lock @@ -1,7 +1,7 @@ %{ "bunch": {:hex, :bunch, "1.6.0", "4775f8cdf5e801c06beed3913b0bd53fceec9d63380cdcccbda6be125a6cfd54", [:mix], [], "hexpm", "ef4e9abf83f0299d599daed3764d19e8eac5d27a5237e5e4d5e2c129cfeb9a22"}, "bunch_native": {:hex, :bunch_native, "0.5.0", "8ac1536789a597599c10b652e0b526d8833348c19e4739a0759a2bedfd924e63", [:mix], [{:bundlex, "~> 1.0", [hex: :bundlex, repo: "hexpm", optional: false]}], "hexpm", "24190c760e32b23b36edeb2dc4852515c7c5b3b8675b1a864e0715bdd1c8f80d"}, - "bundlex": {:hex, :bundlex, "1.2.0", "a89869208a019376a38e8a10e1bd573dcbeae8addd381c2cd74e2817010bef8f", [:mix], [{:bunch, "~> 1.0", [hex: :bunch, repo: "hexpm", optional: false]}, {:qex, "~> 0.5", [hex: :qex, repo: "hexpm", optional: false]}, {:req, "~> 0.4.0", [hex: :req, repo: "hexpm", optional: false]}, {:secure_random, "~> 0.5", [hex: :secure_random, repo: "hexpm", optional: false]}, {:zarex, "~> 1.0", [hex: :zarex, repo: "hexpm", optional: false]}], "hexpm", "d2182b91a2a53847baadf4745ad2291853e786ad28671f474a611e7703dbca9b"}, + "bundlex": {:hex, :bundlex, "1.3.1", "5791b4037df961f092eac9a51d8df91030a80381e442e580a3f4d82c9e5d34f0", [:mix], [{:bunch, "~> 1.0", [hex: :bunch, repo: "hexpm", optional: false]}, {:qex, "~> 0.5", [hex: :qex, repo: "hexpm", optional: false]}, {:req, "~> 0.4.0", [hex: :req, repo: "hexpm", optional: false]}, {:secure_random, "~> 0.5", [hex: :secure_random, repo: "hexpm", optional: false]}, {:zarex, "~> 1.0", [hex: :zarex, repo: "hexpm", optional: false]}], "hexpm", "9651ddc7e627dd1bd0eed9aaaba3de8b4bbc06c10980089f7276cdb82bb3fc51"}, "bunt": {:hex, :bunt, "0.2.1", "e2d4792f7bc0ced7583ab54922808919518d0e57ee162901a16a1b6664ef3b14", [:mix], [], "hexpm", "a330bfb4245239787b15005e66ae6845c9cd524a288f0d141c148b02603777a5"}, "castore": {:hex, :castore, "1.0.4", "ff4d0fb2e6411c0479b1d965a814ea6d00e51eb2f58697446e9c41a97d940b28", [:mix], [], "hexpm", "9418c1b8144e11656f0be99943db4caf04612e3eaecefb5dae9a2a87565584f8"}, "credo": {:hex, :credo, "1.7.0", "6119bee47272e85995598ee04f2ebbed3e947678dee048d10b5feca139435f75", [:mix], [{:bunt, "~> 0.2.1", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "6839fcf63d1f0d1c0f450abc8564a57c43d644077ab96f2934563e68b8a769d7"},