Skip to content

Commit

Permalink
Improve the elixir example
Browse files Browse the repository at this point in the history
  • Loading branch information
LVala committed Nov 27, 2023
1 parent 1672d8d commit 72d5924
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions examples/example.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Peer do

require Logger

alias ExWebRTC.{IceCandidate, PeerConnection, SessionDescription}
alias ExWebRTC.{IceCandidate, PeerConnection, SessionDescription, RTPTransceiver}

@ice_servers [
# %{urls: "stun:stun.stunprotocol.org:3478"},
Expand All @@ -32,7 +32,7 @@ defmodule Peer do

{:ok, pc} = PeerConnection.start_link(ice_servers: @ice_servers)

{:ok, %{conn: conn, stream: stream, peer_connection: pc}}
{:ok, %{conn: conn, stream: stream, peer_connection: pc, other_mid: nil}}

other ->
Logger.error("Couldn't connect to the signalling server: #{inspect(other)}")
Expand All @@ -55,9 +55,10 @@ defmodule Peer do

@impl true
def handle_info({:gun_ws, _, _, {:text, msg}}, state) do
msg
|> Jason.decode!()
|> handle_ws_message(state)
state =
msg
|> Jason.decode!()
|> handle_ws_message(state)

{:noreply, state}
end
Expand Down Expand Up @@ -90,17 +91,22 @@ defmodule Peer do
:gun.ws_send(state.conn, state.stream, {:text, Jason.encode!(msg)})

track = ExWebRTC.MediaStreamTrack.new(:video)
{:ok, transceiver} = PeerConnection.add_transceiver(pc, track)
{:ok, _} = PeerConnection.add_transceiver(pc, track)
{:ok, offer} = PeerConnection.create_offer(pc)
:ok = PeerConnection.set_local_description(pc, offer)
msg = %{"type" => "offer", "sdp" => offer.sdp}
:gun.ws_send(state.conn, state.stream, {:text, Jason.encode!(msg)})

[_, %RTPTransceiver{mid: mid}] = PeerConnection.get_transceivers(pc)

%{state | other_mid: mid}
end

defp handle_ws_message(%{"type" => "answer", "sdp" => sdp}, state) do
Logger.info("Received SDP answer: #{inspect(sdp)}")
answer = %SessionDescription{type: :answer, sdp: sdp}
:ok = PeerConnection.set_remote_description(state.peer_connection, answer)
state
end

defp handle_ws_message(%{"type" => "ice", "data" => data}, state) do
Expand All @@ -114,10 +120,13 @@ defmodule Peer do
}

:ok = PeerConnection.add_ice_candidate(state.peer_connection, candidate)

state
end

defp handle_ws_message(msg, _state) do
defp handle_ws_message(msg, state) do
Logger.info("Received unexpected message: #{inspect(msg)}")
state
end

defp handle_webrtc_message({:ice_candidate, candidate}, state) do
Expand All @@ -132,9 +141,13 @@ defmodule Peer do
:gun.ws_send(state.conn, state.stream, {:text, Jason.encode!(msg)})
end

defp handle_webrtc_message({:rtp, mid, packet}, state) do
defp handle_webrtc_message({:rtp, _mid, _packet}, %{other_mid: nil}) do
Logger.warning("Received RTP, but out transceiver has not beed created")
end

defp handle_webrtc_message({:rtp, _mid, packet}, state) do
Logger.info("Received RTP: #{inspect(packet)}")
PeerConnection.send_rtp(state.peer_connection, "1", packet)
PeerConnection.send_rtp(state.peer_connection, state.other_mid, packet)
end

defp handle_webrtc_message(msg, _state) do
Expand Down

0 comments on commit 72d5924

Please sign in to comment.