Skip to content

Commit 2dc5768

Browse files
authored
Fix port zero when tranceiver stopping (#228)
1 parent 34a4bb0 commit 2dc5768

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

lib/ex_webrtc/peer_connection.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2418,6 +2418,9 @@ defmodule ExWebRTC.PeerConnection do
24182418
# https://www.w3.org/TR/webrtc/#dfn-check-if-negotiation-is-needed
24192419
defp tr_negotiation_needed?([], _), do: false
24202420

2421+
defp tr_negotiation_needed?([tr | _transceivers], _state) when tr.stopping and not tr.stopped,
2422+
do: true
2423+
24212424
defp tr_negotiation_needed?([tr | _transceivers], _state) when tr.mid == nil, do: true
24222425

24232426
defp tr_negotiation_needed?([tr | transceivers], state) do

lib/ex_webrtc/rtp_transceiver.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,11 @@ defmodule ExWebRTC.RTPTransceiver do
434434
mline = to_mline(transceiver, opts)
435435
%ExSDP.Media{mline | port: 0}
436436

437-
transceiver.stopping == true or transceiver.stopped == true ->
437+
transceiver.stopping == true ->
438+
opts = Keyword.put(opts, :direction, :inactive)
439+
to_mline(transceiver, opts)
440+
441+
transceiver.stopped == true ->
438442
opts = Keyword.put(opts, :direction, :inactive)
439443
mline = to_mline(transceiver, opts)
440444
%ExSDP.Media{mline | port: 0}

test/ex_webrtc/peer_connection_test.exs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,59 @@ defmodule ExWebRTC.PeerConnectionTest do
651651
assert sdp.media == []
652652
end
653653

654+
test "after remote offer" do
655+
{:ok, pc1} = PeerConnection.start_link()
656+
{:ok, pc2} = PeerConnection.start_link()
657+
658+
PeerConnection.add_transceiver(pc1, :video)
659+
{:ok, offer} = PeerConnection.create_offer(pc1)
660+
:ok = PeerConnection.set_local_description(pc1, offer)
661+
662+
:ok = PeerConnection.set_remote_description(pc2, offer)
663+
[tr] = PeerConnection.get_transceivers(pc2)
664+
:ok = PeerConnection.stop_transceiver(pc2, tr.id)
665+
666+
assert [
667+
%RTPTransceiver{
668+
current_direction: nil,
669+
direction: :inactive,
670+
stopping: true,
671+
stopped: false
672+
}
673+
] = PeerConnection.get_transceivers(pc2)
674+
675+
{:ok, answer} = PeerConnection.create_answer(pc2)
676+
[video] = ExSDP.parse!(answer.sdp).media
677+
assert video.port == 9
678+
679+
:ok = PeerConnection.set_local_description(pc2, answer)
680+
:ok = PeerConnection.set_remote_description(pc1, answer)
681+
682+
assert_receive {:ex_webrtc, ^pc2, :negotiation_needed}
683+
684+
# Ensure transceiver is removed after renegotiation
685+
686+
{:ok, offer2} = PeerConnection.create_offer(pc2)
687+
:ok = PeerConnection.set_local_description(pc2, offer2)
688+
[new_video] = ExSDP.parse!(offer2.sdp).media
689+
assert new_video.port == 0
690+
691+
assert [
692+
%RTPTransceiver{
693+
current_direction: :inactive,
694+
direction: :inactive,
695+
stopping: true,
696+
stopped: false
697+
}
698+
] = PeerConnection.get_transceivers(pc2)
699+
700+
:ok = PeerConnection.set_remote_description(pc1, offer2)
701+
{:ok, answer2} = PeerConnection.create_answer(pc1)
702+
703+
:ok = PeerConnection.set_remote_description(pc2, answer2)
704+
assert [] = PeerConnection.get_transceivers(pc2)
705+
end
706+
654707
test "with renegotiation" do
655708
{:ok, pc1} = PeerConnection.start_link()
656709
{:ok, pc2} = PeerConnection.start_link()

0 commit comments

Comments
 (0)