Skip to content

Commit

Permalink
Refactor PeerConnection and RTPTransceiver to use new config API
Browse files Browse the repository at this point in the history
  • Loading branch information
LVala committed Jun 20, 2024
1 parent c275561 commit 7939f76
Show file tree
Hide file tree
Showing 9 changed files with 268 additions and 234 deletions.
278 changes: 133 additions & 145 deletions lib/ex_webrtc/peer_connection.ex

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions lib/ex_webrtc/peer_connection/configuration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ defmodule ExWebRTC.PeerConnection.Configuration do

@typedoc false
@type t() :: %__MODULE__{
controlling_process: Process.dest(),
ice_servers: [ice_server()],
ice_transport_policy: :relay | :all,
ice_ip_filter: (:inet.ip_address() -> boolean()),
Expand All @@ -159,6 +160,7 @@ defmodule ExWebRTC.PeerConnection.Configuration do
}

@enforce_keys [
:controlling_process,
:ice_ip_filter,
:audio_extensions,
:video_extensions
Expand Down Expand Up @@ -427,7 +429,7 @@ defmodule ExWebRTC.PeerConnection.Configuration do
Enum.map_reduce(codecs, {free_pts, %{}}, fn codec, {free_pts, mapping} ->
sdp_codecs
|> Enum.find(
&(&1.mime_type == codec.mime_type and
&(String.downcase(&1.mime_type) == String.downcase(codec.mime_type) and
&1.clock_rate == codec.clock_rate and
&1.channels == codec.channels)
)
Expand Down Expand Up @@ -491,7 +493,7 @@ defmodule ExWebRTC.PeerConnection.Configuration do
codecs
|> Enum.find(
# as of now, we ignore sdp_fmtp_line
&(&1.mime_type == sdp_codec.mime_type and
&(String.downcase(&1.mime_type) == String.downcase(sdp_codec.mime_type) and
&1.payload_type == sdp_codec.payload_type and
&1.clock_rate == sdp_codec.clock_rate and
&1.channels == sdp_codec.channels)
Expand All @@ -501,7 +503,7 @@ defmodule ExWebRTC.PeerConnection.Configuration do
[]

other ->
fbs = Enum.filter(sdp_codec.rtcp_fbs, fn fb -> fb in other.rtcp_fbs end)
fbs = Enum.filter(sdp_codec.rtcp_fbs, &(&1 in other.rtcp_fbs))
[%RTPCodecParameters{sdp_codec | rtcp_fbs: fbs}]
end
end)
Expand Down
38 changes: 31 additions & 7 deletions lib/ex_webrtc/rtp_receiver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ defmodule ExWebRTC.RTPReceiver do
track: MediaStreamTrack.t(),
codec: RTPCodecParameters.t() | nil,
simulcast_demuxer: SimulcastDemuxer.t(),
reports?: boolean(),
inbound_rtx?: boolean(),
layers: %{(String.t() | nil) => layer()}
}

Expand All @@ -26,8 +28,8 @@ defmodule ExWebRTC.RTPReceiver do
bytes_received: non_neg_integer(),
packets_received: non_neg_integer(),
markers_received: non_neg_integer(),
report_recorder: ReportRecorder.t(),
nack_generator: NACKGenerator.t()
report_recorder: ReportRecorder.t() | nil,
nack_generator: NACKGenerator.t() | nil
}

@typedoc """
Expand Down Expand Up @@ -56,15 +58,19 @@ defmodule ExWebRTC.RTPReceiver do
end

@doc false
@spec new(MediaStreamTrack.t(), RTPCodecParameters.t() | nil, [ExSDP.Attribute.Extmap.t()]) ::
@spec new(MediaStreamTrack.t(), RTPCodecParameters.t() | nil, [ExSDP.Attribute.Extmap.t()], [
atom()
]) ::
receiver()
def new(track, codec, rtp_hdr_exts) do
def new(track, codec, rtp_hdr_exts, features) do
# layer `nil` is for the packets without RID/ no simulcast
%{
id: Utils.generate_id(),
track: track,
codec: codec,
simulcast_demuxer: SimulcastDemuxer.new(rtp_hdr_exts),
reports?: :reports in features,
inbound_rtx?: :inbound_rtx in features,
layers: %{nil => init_layer(codec)}
}
end
Expand All @@ -80,7 +86,11 @@ defmodule ExWebRTC.RTPReceiver do

layers =
Map.new(receiver.layers, fn {rid, layer} ->
report_recorder = %ReportRecorder{clock_rate: codec && codec.clock_rate}
report_recorder = %ReportRecorder{
layer.report_recorder
| clock_rate: codec && codec.clock_rate
}

{rid, %{layer | report_recorder: report_recorder}}
end)

Expand All @@ -105,8 +115,22 @@ defmodule ExWebRTC.RTPReceiver do
{rid, simulcast_demuxer} = SimulcastDemuxer.demux_packet(receiver.simulcast_demuxer, packet)
layer = receiver.layers[rid] || init_layer(receiver.codec)

report_recorder = ReportRecorder.record_packet(layer.report_recorder, packet)
nack_generator = NACKGenerator.record_packet(layer.nack_generator, packet)
# we only turn off the actual recording when features are not on
# other stuff (like updating some metadata in the recorders etc)
# does not meaningfully impact performance
report_recorder =
if receiver.reports? do
ReportRecorder.record_packet(layer.report_recorder, packet)
else
layer.report_recorder
end

nack_generator =
if receiver.inbound_rtx? do
NACKGenerator.record_packet(layer.nack_generator, packet)
else
layer.nack_generator
end

layer = %{
layer
Expand Down
24 changes: 20 additions & 4 deletions lib/ex_webrtc/rtp_sender.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ defmodule ExWebRTC.RTPSender do
packets_sent: non_neg_integer(),
bytes_sent: non_neg_integer(),
markers_sent: non_neg_integer(),
reports?: boolean(),
outbound_rtx?: boolean(),
report_recorder: ReportRecorder.t(),
nack_responder: NACKResponder.t()
}
Expand Down Expand Up @@ -62,9 +64,10 @@ defmodule ExWebRTC.RTPSender do
[Extmap.t()],
String.t() | nil,
non_neg_integer() | nil,
non_neg_integer() | nil
non_neg_integer() | nil,
[atom()]
) :: sender()
def new(track, codec, rtx_codec, rtp_hdr_exts, mid \\ nil, ssrc, rtx_ssrc) do
def new(track, codec, rtx_codec, rtp_hdr_exts, mid, ssrc, rtx_ssrc, features) do
# convert to a map to be able to find extension id using extension uri
rtp_hdr_exts = Map.new(rtp_hdr_exts, fn extmap -> {extmap.uri, extmap} end)
# TODO: handle cases when codec == nil (no valid codecs after negotiation)
Expand All @@ -84,6 +87,8 @@ defmodule ExWebRTC.RTPSender do
packets_sent: 0,
bytes_sent: 0,
markers_sent: 0,
reports?: :reports in features,
outbound_rtx?: :outbound_rtx in features,
report_recorder: %ReportRecorder{clock_rate: codec && codec.clock_rate},
nack_responder: %NACKResponder{}
}
Expand Down Expand Up @@ -138,8 +143,19 @@ defmodule ExWebRTC.RTPSender do
|> ExRTP.Packet.remove_extension(mid_extmap.id)
|> ExRTP.Packet.add_extension(mid_ext)

report_recorder = ReportRecorder.record_packet(sender.report_recorder, packet)
nack_responder = NACKResponder.record_packet(sender.nack_responder, packet)
report_recorder =
if sender.reports? do
ReportRecorder.record_packet(sender.report_recorder, packet)
else
sender.report_recorder
end

nack_responder =
if sender.outbound_rtx? do
NACKResponder.record_packet(sender.nack_responder, packet)
else
sender.nack_responder
end

data = ExRTP.Packet.encode(packet)

Expand Down
Loading

0 comments on commit 7939f76

Please sign in to comment.