Skip to content

Commit

Permalink
Add send_pli function (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
LVala committed Apr 15, 2024
1 parent 7cfac96 commit b6069d9
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/ex_webrtc/peer_connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,14 @@ defmodule ExWebRTC.PeerConnection do
GenServer.cast(peer_connection, {:send_rtp, track_id, packet})
end

@doc """
Send an RTCP PLI feedback to the remote peer using specified track.
"""
@spec send_pli(peer_connection(), MediaStreamTrack.id()) :: :ok
def send_pli(peer_connection, track_id) do
GenServer.cast(peer_connection, {:send_pli, track_id})
end

@doc """
Returns peer connection's statistics.
Expand Down Expand Up @@ -801,6 +809,28 @@ defmodule ExWebRTC.PeerConnection do
end
end

@impl true
def handle_cast({:send_pli, track_id}, state) do
receiver =
state.transceivers
|> Enum.find_value(fn
%{receiver: %{track: %{id: ^track_id}} = receiver} -> receiver
_ -> nil
end)

if receiver.ssrc != nil do
encoded =
%ExRTCP.Packet.PayloadFeedback.PLI{sender_ssrc: 1, media_ssrc: receiver.ssrc}
|> ExRTCP.Packet.encode()

:ok = DTLSTransport.send_rtcp(state.dtls_transport, encoded)
else
Logger.warning("Attempted to send PLI for non existent track")
end

{:noreply, state}
end

@impl true
def handle_info({:ex_ice, _from, {:connection_state_change, new_ice_state}}, state) do
state = %{state | ice_state: new_ice_state}
Expand Down

0 comments on commit b6069d9

Please sign in to comment.