Skip to content

Commit 1a25130

Browse files
committed
Don't forward user's RTP header extensions in send_rtp/4
1 parent d74a937 commit 1a25130

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

lib/ex_webrtc/peer_connection.ex

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,16 @@ defmodule ExWebRTC.PeerConnection do
142142
GenServer.call(peer_connection, {:controlling_process, controlling_process})
143143
end
144144

145+
@doc """
146+
Returns current `peer_connection` configuration.
147+
148+
Note: the configuration may change after applying remote description.
149+
"""
150+
@spec get_configuration(peer_connection()) :: Configuration.t()
151+
def get_configuration(peer_connection) do
152+
GenServer.call(peer_connection, :get_configuration)
153+
end
154+
145155
@doc """
146156
Sends an RTP packet to the remote peer using the track specified by the `track_id`.
147157
@@ -561,6 +571,11 @@ defmodule ExWebRTC.PeerConnection do
561571
{:reply, :ok, state}
562572
end
563573

574+
@impl true
575+
def handle_call(:get_configuration, _from, state) do
576+
{:reply, state.config, state}
577+
end
578+
564579
@impl true
565580
def handle_call(:get_connection_state, _from, state) do
566581
{:reply, state.conn_state, state}
@@ -1097,6 +1112,12 @@ defmodule ExWebRTC.PeerConnection do
10971112
|> Enum.find(fn {tr, _idx} -> tr.sender.track && tr.sender.track.id == track_id end)
10981113
|> case do
10991114
{tr, idx} ->
1115+
# When RTP packet comes from another peer connection,
1116+
# it can already have some extensions, in particular,
1117+
# extensions that we didn't negotiate (consider simulcast and rid).
1118+
# Remove these extensions and add ours.
1119+
packet = ExRTP.Packet.remove_extensions(packet)
1120+
11001121
{packet, state} =
11011122
case state.twcc_extension_id do
11021123
nil ->
@@ -1107,10 +1128,7 @@ defmodule ExWebRTC.PeerConnection do
11071128
ExRTP.Packet.Extension.TWCC.new(state.sent_packets)
11081129
|> ExRTP.Packet.Extension.TWCC.to_raw(id)
11091130

1110-
packet =
1111-
packet
1112-
|> ExRTP.Packet.remove_extension(id)
1113-
|> ExRTP.Packet.add_extension(twcc)
1131+
packet = ExRTP.Packet.add_extension(packet, twcc)
11141132

11151133
state = %{state | sent_packets: state.sent_packets + 1 &&& 0xFFFF}
11161134
{packet, state}

lib/ex_webrtc/peer_connection/configuration.ex

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,12 @@ defmodule ExWebRTC.PeerConnection.Configuration do
158158
rtcp_feedbacks: [rtcp_feedback()]
159159
]
160160

161-
@typedoc false
161+
@typedoc """
162+
`ExWebRTC.PeerConnection` configuration.
163+
164+
It is created from options passed to `ExWebRTC.PeerConnection.start_link/1`.
165+
See `t:options/0` for more.
166+
"""
162167
@type t() :: %__MODULE__{
163168
controlling_process: Process.dest(),
164169
ice_servers: [ice_server()],

test/ex_webrtc/peer_connection_test.exs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,13 @@ defmodule ExWebRTC.PeerConnectionTest do
227227
:ok = negotiate(pc1, pc2)
228228
:ok = connect(pc1, pc2)
229229

230+
config = PeerConnection.get_configuration(pc1)
231+
negotiated_ext_ids = Enum.map(config.audio_extensions ++ config.video_extensions, & &1.id)
232+
non_negotiated_ext_id = Enum.random(Enum.to_list(1..255) -- negotiated_ext_ids)
233+
230234
payload = <<3, 2, 5>>
231-
packet = ExRTP.Packet.new(payload)
235+
non_negotiated_ext = %ExRTP.Packet.Extension{id: non_negotiated_ext_id, data: "0"}
236+
packet = payload |> ExRTP.Packet.new() |> ExRTP.Packet.add_extension(non_negotiated_ext)
232237

233238
# Try to send data using correct track id.
234239
# Assert that pc2 received this data and send_rtp didn't modify some of the fields.
@@ -237,6 +242,7 @@ defmodule ExWebRTC.PeerConnectionTest do
237242
assert recv_packet.payload == packet.payload
238243
assert recv_packet.sequence_number == packet.sequence_number
239244
assert recv_packet.timestamp == packet.timestamp
245+
assert non_negotiated_ext not in recv_packet.extensions
240246

241247
# Try to send data using invalid track id.
242248
# Assert that pc1 is still alive and pc2 didn't receive this data.

0 commit comments

Comments
 (0)