Skip to content

Commit

Permalink
Fix simulcast issues
Browse files Browse the repository at this point in the history
  • Loading branch information
LVala committed Jun 21, 2024
1 parent e6c8dad commit c65e1cb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 27 deletions.
26 changes: 13 additions & 13 deletions lib/ex_webrtc/peer_connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1040,21 +1040,21 @@ defmodule ExWebRTC.PeerConnection do

@impl true
def handle_cast({:send_pli, track_id, rid}, state) do
receiver =
state.transceivers
|> Enum.find_value(fn
%{receiver: %{track: %{id: ^track_id}} = receiver} -> receiver
_ -> nil
end)
state.transceivers
|> Enum.find(fn tr -> tr.receiver.track.id == track_id end)
|> case do
%{receiver: %{layers: %{^rid => %{ssrc: ssrc}}}} when ssrc != nil ->
encoded =
%ExRTCP.Packet.PayloadFeedback.PLI{sender_ssrc: 1, media_ssrc: ssrc}
|> ExRTCP.Packet.encode()

if receiver[rid] != nil and receiver[rid].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)

:ok = DTLSTransport.send_rtcp(state.dtls_transport, encoded)
else
Logger.warning("Attempted to send PLI for non existent track")
nil ->
Logger.warning("Attempted to send PLI for non existent track #{inspect(track_id)}")

_other ->
Logger.warning("Unable to send PLI for track #{inspect(track_id)}, rid #{inspect(rid)}")
end

{:noreply, state}
Expand Down
6 changes: 4 additions & 2 deletions lib/ex_webrtc/rtp_receiver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ defmodule ExWebRTC.RTPReceiver do
track: track,
codec: codec,
simulcast_demuxer: SimulcastDemuxer.new(rtp_hdr_exts),
layers: %{nil => init_layer(codec)}
layers: %{}
}
end

Expand Down Expand Up @@ -213,8 +213,10 @@ defmodule ExWebRTC.RTPReceiver do
@spec get_stats(receiver(), non_neg_integer()) :: [map()]
def get_stats(receiver, timestamp) do
Enum.map(receiver.layers, fn {rid, layer} ->
id = if(rid == nil, do: receiver.track.id, else: "#{receiver.track.id}, rid: #{rid}")

%{
id: receiver.track.id,
id: id,
rid: rid,
type: :inbound_rtp,
timestamp: timestamp,
Expand Down
13 changes: 1 addition & 12 deletions test/ex_webrtc/rtp_receiver_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,7 @@ defmodule ExWebRTC.RTPReceiverTest do
track = MediaStreamTrack.new(:audio)
receiver = RTPReceiver.new(track, @codec, [])

assert [
%{
id: receiver.track.id,
rid: nil,
type: :inbound_rtp,
timestamp: timestamp,
ssrc: nil,
bytes_received: 0,
packets_received: 0,
markers_received: 0
}
] == RTPReceiver.get_stats(receiver, timestamp)
assert [] == RTPReceiver.get_stats(receiver, timestamp)

packet1 = Packet.new(payload, ssrc: 1234)
raw_packet1 = Packet.encode(packet1)
Expand Down

0 comments on commit c65e1cb

Please sign in to comment.