Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mickel8 committed Oct 18, 2023
1 parent 0e615c5 commit c28efe0
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions lib/ex_webrtc/peer_connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -293,16 +293,26 @@ defmodule ExWebRTC.PeerConnection do
defp apply_remote_description(_type, sdp, state) do
# TODO apply steps listed in RFC 8829 5.10
media = hd(sdp.media)
{:ice_ufrag, ufrag} = ExSDP.Media.get_attribute(media, :ice_ufrag)
{:ice_pwd, pwd} = ExSDP.Media.get_attribute(media, :ice_pwd)

:ok = ICEAgent.set_remote_credentials(state.ice_agent, ufrag, pwd)
:ok = ICEAgent.gather_candidates(state.ice_agent)
with {:ice_ufrag, ufrag} <- ExSDP.Media.get_attribute(media, :ice_ufrag),
{:ice_pwd, pwd} <- ExSDP.Media.get_attribute(media, :ice_pwd),
{:ok, transceivers} <- get_transceivers(sdp, state) do
:ok = ICEAgent.set_remote_credentials(state.ice_agent, ufrag, pwd)
:ok = ICEAgent.gather_candidates(state.ice_agent)

transceivers = Map.merge(transceivers, state.transceivers)
{:ok, %{state | current_remote_desc: sdp, transceivers: transceivers}}
else
nil -> {:error, :missing_ice_ufrag_or_pwd}
end
end

defp get_transceivers(sdp, state) do
transceivers =
sdp.media
|> Enum.reduce(%{}, fn media ->
{:mid, mid} = ExSDP.Media.get_attribute(media, :mid)
Enum.reduce_while(sdp.media, %{}, fn media, transceivers ->

with {:mid, mid} <- ExSDP.Media.get_attribute(media, :mid),
direction when direction != :inactive <- get_media_direction(media) || :sendrecv do
# if there is no direction, the default is sendrecv
# see RFC 3264, sec. 6.1
direction = get_media_direction(media) || :sendrecv
Expand All @@ -320,17 +330,15 @@ defmodule ExWebRTC.PeerConnection do

{tr.mid, tr}
end)
|> Map.merge(state.transceivers)

{:ok, %{state | current_remote_desc: sdp, transceivers: transceivers}}
end

defp find_or_create_transceiver(mid, direction, transceivers)
when direction in [:sendrecv, :recvonly] do
Enum.find(transceivers, %RTPTransceiver{mid: mid, direction: :recvonly}, fn
{nil, tr} when tr.direction == direction -> tr
_other -> nil
Enum.find(transceivers, {nil, %RTPTransceiver{mid: mid, direction: :recvonly}}, fn
{nil, tr} -> tr.direction == direction
_other -> false
end)
|> elem(1)
end

defp find_or_create_transceiver(mid, :sendonly, _transceivers) do
Expand Down

0 comments on commit c28efe0

Please sign in to comment.