diff --git a/lib/ex_webrtc/peer_connection.ex b/lib/ex_webrtc/peer_connection.ex index 13c5a33..7a942af 100644 --- a/lib/ex_webrtc/peer_connection.ex +++ b/lib/ex_webrtc/peer_connection.ex @@ -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} diff --git a/lib/ex_webrtc/rtp_receiver.ex b/lib/ex_webrtc/rtp_receiver.ex index 01ae8da..5f111ac 100644 --- a/lib/ex_webrtc/rtp_receiver.ex +++ b/lib/ex_webrtc/rtp_receiver.ex @@ -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 @@ -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, diff --git a/test/ex_webrtc/rtp_receiver_test.exs b/test/ex_webrtc/rtp_receiver_test.exs index 9a34b86..0b053bf 100644 --- a/test/ex_webrtc/rtp_receiver_test.exs +++ b/test/ex_webrtc/rtp_receiver_test.exs @@ -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)