From 429273550237e7aa7358c11647becbd8edc8c10a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wala?= Date: Wed, 25 Oct 2023 11:45:31 +0200 Subject: [PATCH] Add initial implementation of `create_offer` and `add_transceiver` --- lib/ex_webrtc/peer_connection.ex | 157 +++++++++++++++++++++++++++++-- lib/ex_webrtc/rtp_transceiver.ex | 31 +++--- mix.exs | 3 +- mix.lock | 2 +- 4 files changed, 171 insertions(+), 22 deletions(-) diff --git a/lib/ex_webrtc/peer_connection.ex b/lib/ex_webrtc/peer_connection.ex index a0be88ac..36652cc1 100644 --- a/lib/ex_webrtc/peer_connection.ex +++ b/lib/ex_webrtc/peer_connection.ex @@ -22,6 +22,12 @@ defmodule ExWebRTC.PeerConnection do @type offer_options() :: [ice_restart: boolean()] @type answer_options() :: [] + @type transceiver_options() :: [ + direction: RTPTransceiver.direction(), + send_encodings: [:TODO], + streams: [:TODO] + ] + @enforce_keys [:config, :owner] defstruct @enforce_keys ++ [ @@ -35,7 +41,10 @@ defmodule ExWebRTC.PeerConnection do :dtls_buffered_packets, dtls_finished: false, transceivers: [], - signaling_state: :stable + signaling_state: :stable, + next_mid: 0, + last_offer: nil, + last_answer: nil ] #### API #### @@ -83,6 +92,15 @@ defmodule ExWebRTC.PeerConnection do GenServer.call(peer_connection, :get_transceivers) end + @spec add_transceiver( + peer_connection(), + RTPTransceiver.kind() | MediaStreamTrack.t(), + transceiver_options() + ) :: {:ok, RTPTransceiver.t()} | {:error, :TODO} + def add_transceiver(peer_connection, track_or_kind, options \\ []) do + GenServer.call(peer_connection, {:add_transceiver, track_or_kind, options}) + end + #### CALLBACKS #### @impl true @@ -109,9 +127,68 @@ defmodule ExWebRTC.PeerConnection do {:ok, state} end + @impl true + def handle_call({:create_offer, options}, _from, state) + when state.signaling_state in [:stable, :have_local_offer, :have_remote_pranswer] do + # TODO: handle subsequent offers + + if Keyword.get(options, :ice_restart, false) do + ICEAgent.restart(state.ice_agent) + end + + # we need to asign unique mid values for the transceivers + # in this case internal counter is used + + # TODO: set counter so its greater than any mid in remote description or own transcevers mids + next_mid = find_next_mid(state.next_mid) + {transceivers, next_mid} = assign_mids(state.transceivers, next_mid) + + {:ok, ice_ufrag, ice_pwd} = ICEAgent.get_local_credentials(state.ice_agent) + {:ok, dtls_fingerprint} = ExDTLS.get_cert_fingerprint(state.dtls_client) + + offer = + %ExSDP{ExSDP.new() | timing: %ExSDP.Timing{start_time: 0, stop_time: 0}} + |> ExSDP.add_attribute({:ice_options, "trickle"}) + + config = + [ + ice_ufrag: ice_ufrag, + ice_pwd: ice_pwd, + ice_options: "trickle", + fingerprint: {:sha256, Utils.hex_dump(dtls_fingerprint)}, + setup: :actpass, + rtcp: true + ] + + mlines = + Enum.map(transceivers, fn transceiver -> + RTPTransceiver.to_mline(transceiver, config) + end) + + mids = + Enum.map(mlines, fn mline -> + {:mid, mid} = ExSDP.Media.get_attribute(mline, :mid) + mid + end) + + offer = + offer + |> ExSDP.add_attributes([ + %ExSDP.Attribute.Group{semantics: "BUNDLE", mids: mids}, + "extmap-allow-mixed" + ]) + |> ExSDP.add_media(mlines) + + sdp = to_string(offer) + desc = %SessionDescription{type: :offer, sdp: sdp} + state = %{state | next_mid: next_mid, last_offer: sdp} + + {:reply, {:ok, desc}, state} + end + @impl true def handle_call({:create_offer, _options}, _from, state) do - {:reply, :ok, state} + {:reply, {:error, :invalid_state}, state} end @impl true @@ -155,18 +232,37 @@ defmodule ExWebRTC.PeerConnection do ]) |> ExSDP.add_media(mlines) - desc = %SessionDescription{type: :answer, sdp: to_string(answer)} + sdp = to_string(answer) + desc = %SessionDescription{type: :answer, sdp: sdp} + state = %{state | last_answer: sdp} + {:reply, {:ok, desc}, state} end + @impl true def handle_call({:create_answer, _options}, _from, state) do {:reply, {:error, :invalid_state}, state} end @impl true - def handle_call({:set_local_description, _desc}, _from, state) do - # temporary, so the dialyzer will shut up - maybe_next_state(:stable, :local, :offer) + def handle_call({:set_local_description, desc}, _from, state) do + %SessionDescription{type: type, sdp: sdp} = desc + + case type do + :rollback -> + {:reply, :ok, state} + + other_type -> + with {:ok, next_state} <- maybe_next_state(state.signaling_state, :local, other_type), + :ok <- check_desc_altered(type, sdp, state), + {:ok, sdp} <- ExSDP.parse(sdp), + {:ok, state} <- apply_local_description(other_type, sdp, state) do + {:reply, :ok, %{state | signaling_state: next_state}} + else + error -> {:reply, error, state} + end + end + {:reply, :ok, state} end @@ -189,6 +285,7 @@ defmodule ExWebRTC.PeerConnection do end end + @impl true def handle_call({:add_ice_candidate, candidate}, _from, state) do with "candidate:" <> attr <- candidate.candidate do ICEAgent.add_remote_candidate(state.ice_agent, attr) @@ -197,10 +294,31 @@ defmodule ExWebRTC.PeerConnection do {:reply, :ok, state} end + @impl true def handle_call(:get_transceivers, _from, state) do {:reply, state.transceivers, state} end + @impl true + def handle_call({:add_transceiver, :audio, options}, _from, state) do + # TODO: proper implementation, change the :audio above to track_or_kind + direction = Keyword.get(options, :direction, :sendrcv) + + # hardcoded audio codec + codecs = [ + %ExWebRTC.RTPCodecParameters{ + payload_type: 111, + mime_type: "audio/opus", + clock_rate: 48_000, + channels: 2 + } + ] + + transceiver = %RTPTransceiver{mid: nil, direction: direction, kind: :audio, codecs: codecs} + transceivers = List.insert_at(state.transceivers, -1, transceiver) + {:reply, {:ok, transceiver}, %{state | transceivers: transceivers}} + end + @impl true def handle_info({:ex_ice, _from, :connected}, state) do if state.dtls_buffered_packets do @@ -278,6 +396,11 @@ defmodule ExWebRTC.PeerConnection do {:noreply, state} end + defp apply_local_description(_type, _sdp, state) do + # TODO: implement + {:ok, state} + end + defp apply_remote_description(_type, sdp, state) do # TODO apply steps listed in RFC 8829 5.10 media = hd(sdp.media) @@ -325,6 +448,28 @@ defmodule ExWebRTC.PeerConnection do end) end + defp assign_mids(transceivers, next_mid, acc \\ []) + defp assign_mids([], next_mid, acc), do: {Enum.reverse(acc), next_mid} + + defp assign_mids([transceiver | rest], next_mid, acc) when is_nil(transceiver.mid) do + transceiver = %RTPTransceiver{transceiver | mid: Integer.to_string(next_mid)} + assign_mids(rest, next_mid + 1, [transceiver | acc]) + end + + defp assign_mids([transceiver | rest], next_mid, acc) do + assign_mids(rest, next_mid, [transceiver | acc]) + end + + defp find_next_mid(next_mid) do + # TODO: implement + next_mid + end + + defp check_desc_altered(:offer, sdp, %{last_offer: offer}) when sdp == offer, do: :ok + defp check_desc_altered(:offer, _sdp, _state), do: {:error, :offer_altered} + defp check_desc_altered(:answer, sdp, %{last_answer: answer}) when sdp == answer, do: :ok + defp check_desc_altered(:answer, _sdp, _state), do: {:error, :answer_altered} + # Signaling state machine, RFC 8829 3.2 defp maybe_next_state(:stable, :remote, :offer), do: {:ok, :have_remote_offer} defp maybe_next_state(:stable, :local, :offer), do: {:ok, :have_local_offer} diff --git a/lib/ex_webrtc/rtp_transceiver.ex b/lib/ex_webrtc/rtp_transceiver.ex index 5ad1d49b..bdb4498e 100644 --- a/lib/ex_webrtc/rtp_transceiver.ex +++ b/lib/ex_webrtc/rtp_transceiver.ex @@ -5,10 +5,13 @@ defmodule ExWebRTC.RTPTransceiver do alias ExWebRTC.{RTPCodecParameters, RTPReceiver} + @type direction() :: :sendonly | :recvonly | :sendrecv | :inactive | :stopped + @type kind() :: :audio | :video + @type t() :: %__MODULE__{ mid: String.t(), - direction: :sendonly | :recvonly | :sendrecv | :inactive | :stopped, - kind: :audio | :video, + direction: direction(), + kind: kind(), hdr_exts: [], codecs: [], rtp_receiver: nil @@ -69,23 +72,25 @@ defmodule ExWebRTC.RTPTransceiver do [rtp_mapping, codec.sdp_fmtp_line, codec.rtcp_fbs] end) + attributes = + [ + transceiver.direction, + {:mid, transceiver.mid}, + {:ice_ufrag, Keyword.fetch!(config, :ice_ufrag)}, + {:ice_pwd, Keyword.fetch!(config, :ice_pwd)}, + {:ice_options, Keyword.fetch!(config, :ice_options)}, + {:fingerprint, Keyword.fetch!(config, :fingerprint)}, + {:setup, Keyword.fetch!(config, :setup)}, + :rtcp_mux + ] ++ if(Keyword.get(config, :rtcp, false), do: [{"rtcp", "9 IN IP4 0.0.0.0"}], else: []) + %ExSDP.Media{ ExSDP.Media.new(transceiver.kind, 9, "UDP/TLS/RTP/SAVPF", pt) | # mline must be followed by a cline, which must contain # the default value "IN IP4 0.0.0.0" (as there are no candidates yet) connection_data: [%ExSDP.ConnectionData{address: {0, 0, 0, 0}}] } - |> ExSDP.Media.add_attributes([ - transceiver.direction, - {:mid, transceiver.mid}, - {:ice_ufrag, Keyword.fetch!(config, :ice_ufrag)}, - {:ice_pwd, Keyword.fetch!(config, :ice_pwd)}, - {:ice_options, Keyword.fetch!(config, :ice_options)}, - {:fingerprint, Keyword.fetch!(config, :fingerprint)}, - {:setup, Keyword.fetch!(config, :setup)}, - :rtcp_mux - ]) - |> ExSDP.Media.add_attributes(media_formats) + |> ExSDP.Media.add_attributes(attributes ++ media_formats) end defp update(transceiver, mline) do diff --git a/mix.exs b/mix.exs index 509b39a3..bcf27219 100644 --- a/mix.exs +++ b/mix.exs @@ -45,8 +45,7 @@ defmodule ExWebRTC.MixProject do defp deps do [ - # {:ex_sdp, "~> 0.11"}, - {:ex_sdp, github: "membraneframework/ex_sdp", branch: "get-attr"}, + {:ex_sdp, "~> 0.13"}, {:ex_ice, "~> 0.1"}, {:ex_dtls, "~> 0.13"}, diff --git a/mix.lock b/mix.lock index fdacdc0b..913fadc4 100644 --- a/mix.lock +++ b/mix.lock @@ -11,7 +11,7 @@ "ex_doc": {:hex, :ex_doc, "0.30.6", "5f8b54854b240a2b55c9734c4b1d0dd7bdd41f71a095d42a70445c03cf05a281", [:mix], [{:earmark_parser, "~> 1.4.31", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "bd48f2ddacf4e482c727f9293d9498e0881597eae6ddc3d9562bd7923375109f"}, "ex_dtls": {:hex, :ex_dtls, "0.13.0", "4d7631eefc19a8820d4f79883f379ff2ad642976bda55493d4ec4e5d10d6c078", [:mix], [{:unifex, "~> 1.0", [hex: :unifex, repo: "hexpm", optional: false]}], "hexpm", "3ece30967006ec12a4088e60514cb08847814fba8b8a21aca3862e5d1fd4a6bc"}, "ex_ice": {:hex, :ex_ice, "0.1.0", "2653c884872d8769cf9fc655c74002a63ed6c21be1b3c2badfa42bdc74de2355", [:mix], [{:ex_stun, "~> 0.1.0", [hex: :ex_stun, repo: "hexpm", optional: false]}], "hexpm", "e2539a321f87f31997ba974d532d00511e5828f2f113b550b1ef6aa799dd2ffe"}, - "ex_sdp": {:git, "https://github.com/membraneframework/ex_sdp.git", "2d8dc9e2c964ed2d883a21d88547e9c5aaf0df1a", [branch: "get-attr"]}, + "ex_sdp": {:hex, :ex_sdp, "0.13.0", "b464cf5f6b70433159be243115857599f82b07234ee022997868c85ae1f225f7", [:mix], [{:bunch, "~> 1.3", [hex: :bunch, repo: "hexpm", optional: false]}, {:uuid, "~> 1.1", [hex: :uuid, repo: "hexpm", optional: false]}], "hexpm", "975ca4d274240c51ee85909bc0618bd4dd940e69f7d8c8f0d701f1524eaffaad"}, "ex_stun": {:hex, :ex_stun, "0.1.0", "252474bf4c8519fbf4bc0fbfc6a1b846a634b1478c65dbbfb4b6ab4e33c2a95a", [:mix], [], "hexpm", "629fc8be45b624a92522f81d85ba001877b1f0745889a2419bdb678790d7480c"}, "excoveralls": {:hex, :excoveralls, "0.17.1", "83fa7906ef23aa7fc8ad7ee469c357a63b1b3d55dd701ff5b9ce1f72442b2874", [:mix], [{:castore, "~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "95bc6fda953e84c60f14da4a198880336205464e75383ec0f570180567985ae0"}, "file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},