Skip to content

Commit f5d76ea

Browse files
committed
wip
1 parent 945615b commit f5d76ea

File tree

4 files changed

+50
-10
lines changed

4 files changed

+50
-10
lines changed

lib/ex_webrtc/peer_connection.ex

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,12 @@ defmodule ExWebRTC.PeerConnection do
322322

323323
mids =
324324
Enum.map(mlines, fn mline ->
325-
{:mid, mid} = ExSDP.get_attribute(mline, :mid)
326-
mid
325+
unless SDPUtils.rejected?(mline) do
326+
{:mid, mid} = ExSDP.get_attribute(mline, :mid)
327+
mid
328+
end
327329
end)
330+
|> Enum.reject(&(&1 == nil))
328331

329332
offer =
330333
offer
@@ -381,9 +384,12 @@ defmodule ExWebRTC.PeerConnection do
381384

382385
mids =
383386
Enum.map(mlines, fn mline ->
384-
{:mid, mid} = ExSDP.get_attribute(mline, :mid)
385-
mid
387+
unless SDPUtils.rejected?(mline) do
388+
{:mid, mid} = ExSDP.get_attribute(mline, :mid)
389+
mid
390+
end
386391
end)
392+
|> Enum.reject(&(&1 == nil))
387393

388394
answer =
389395
answer

lib/ex_webrtc/sdp_utils.ex

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
defmodule ExWebRTC.SDPUtils do
22
@moduledoc false
33

4+
alias ExWebRTC.SDPUtils
45
alias ExRTP.Packet.Extension
56
alias ExSDP.Attribute.Extmap
67

@@ -44,9 +45,13 @@ defmodule ExWebRTC.SDPUtils do
4445

4546
mline_mids =
4647
Enum.map(sdp.media, fn media ->
47-
{:mid, mid} = ExSDP.get_attribute(media, :mid)
48-
mid
48+
# rejected m-lines are not included in the BUNDLE group
49+
unless SDPUtils.rejected?(media) do
50+
{:mid, mid} = ExSDP.get_attribute(media, :mid)
51+
mid
52+
end
4953
end)
54+
|> Enum.reject(&(&1 == nil))
5055

5156
case filter_groups(groups, "BUNDLE") do
5257
[%ExSDP.Attribute.Group{semantics: "BUNDLE", mids: group_mids}] ->
@@ -129,9 +134,21 @@ defmodule ExWebRTC.SDPUtils do
129134

130135
@spec add_ice_candidates(ExSDP.t(), [String.t()]) :: ExSDP.t()
131136
def add_ice_candidates(sdp, candidates) do
137+
# we only add candidates to the first mline
138+
# as we don't support bundle-policies other than "max-bundle"
132139
candidates = Enum.map(candidates, &{"candidate", &1})
133-
media = Enum.map(sdp.media, &ExSDP.add_attributes(&1, candidates))
134-
%ExSDP{sdp | media: media}
140+
141+
if sdp.media != [] do
142+
mline =
143+
sdp.media
144+
|> List.first()
145+
|> ExSDP.add_attribute(candidates)
146+
147+
media = List.replace_at(sdp.media, 0, mline)
148+
%ExSDP{sdp | media: media}
149+
else
150+
sdp
151+
end
135152
end
136153

137154
@spec get_dtls_role(ExSDP.t()) ::
@@ -284,7 +301,13 @@ defmodule ExWebRTC.SDPUtils do
284301
end
285302

286303
@spec rejected?(ExSDP.Media.t()) :: boolean()
287-
def rejected?(%ExSDP.Media{port: 0}), do: true
304+
def rejected?(%ExSDP.Media{port: 0} = media) do
305+
# An m-line is only rejected when the port is set to 0,
306+
# and there is no `bundle-only` attribute.
307+
# See RFC 8843, sec. 6.
308+
"bundle-only" not in media.attributes
309+
end
310+
288311
def rejected?(%ExSDP.Media{}), do: false
289312

290313
defp do_get_ice_credentials(sdp_or_mline) do

test/ex_webrtc/peer_connection_test.exs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,12 @@ defmodule ExWebRTC.PeerConnectionTest do
276276
test "BUNDLE group" do
277277
{:ok, pc} = PeerConnection.start_link()
278278

279-
sdp = ExSDP.add_media(ExSDP.new(), [@audio_mline, @video_mline])
279+
rejected_mline =
280+
%ExSDP.Media{@audio_mline | port: 0}
281+
|> ExSDP.delete_attribute(:mid)
282+
|> ExSDP.add_attribute({:mid, "2"})
283+
284+
sdp = ExSDP.add_media(ExSDP.new(), [@audio_mline, @video_mline, rejected_mline])
280285

281286
[
282287
{[], {:error, :missing_bundle_group}},

test/ex_webrtc/sdp_utils_test.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,11 @@ defmodule ExWebRTC.SDPUtilsTest do
99

1010
mline = ExSDP.Media.new(:audio, 9, "UDP/TLS/RTP/SAVPF", [8])
1111
assert false == SDPUtils.rejected?(mline)
12+
13+
mline =
14+
ExSDP.Media.new(:audio, 0, "UDP/TLS/RTP/SAVPF", [8])
15+
|> ExSDP.add_attribute("bundle-only")
16+
17+
assert false == SDPUtils.rejected?(mline)
1218
end
1319
end

0 commit comments

Comments
 (0)