Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mickel8 committed Jan 15, 2025
1 parent 008b334 commit a644896
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 19 deletions.
13 changes: 5 additions & 8 deletions lib/ex_webrtc/rtp_sender.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule ExWebRTC.RTPSender do
mid: String.t() | nil,
pt: non_neg_integer() | nil,
rtx_pt: non_neg_integer() | nil,
# ssrc and rtx_ssrc are always present, even if there is no track
# ssrc and rtx_ssrc are always present, even if there is no track,
# or transceiver direction is recvonly.
# We preallocate them so they can be included in SDP when needed.
ssrc: non_neg_integer(),
Expand Down Expand Up @@ -140,8 +140,8 @@ defmodule ExWebRTC.RTPSender do
msid_attrs =
case sender.track do
nil ->
# In theory, we should do this "for each MediaStream that was associated with the transceiver"
# but web browsers (chrome, ff), include MSID even when there aren't any MediaStreams
# In theory, we should do this "for each MediaStream that was associated with the transceiver",
# but web browsers (chrome, ff) include MSID even when there aren't any MediaStreams
[ExSDP.Attribute.MSID.new("-", nil)]

%MediaStreamTrack{streams: streams} ->
Expand Down Expand Up @@ -192,13 +192,10 @@ defmodule ExWebRTC.RTPSender do
streams ->
{ssrc_attrs, rtx_ssrc_attrs} =
Enum.reduce(streams, {[], []}, fn stream, {ssrc_attrs, rtx_ssrc_attrs} ->
ssrc_attr = [%ExSDP.Attribute.SSRC{id: ssrc, attribute: "msid", value: stream}]
ssrc_attr = %ExSDP.Attribute.SSRC{id: ssrc, attribute: "msid", value: stream}
ssrc_attrs = [ssrc_attr | ssrc_attrs]

rtx_ssrc_attr = [
%ExSDP.Attribute.SSRC{id: rtx_ssrc, attribute: "msid", value: stream}
]

rtx_ssrc_attr = %ExSDP.Attribute.SSRC{id: rtx_ssrc, attribute: "msid", value: stream}
rtx_ssrc_attrs = [rtx_ssrc_attr | rtx_ssrc_attrs]

{ssrc_attrs, rtx_ssrc_attrs}
Expand Down
92 changes: 81 additions & 11 deletions test/ex_webrtc/rtp_sender_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,30 @@ defmodule ExWebRTC.RTPSenderTest do
@ssrc 354_947
@rtx_ssrc 123_455

setup do
track = MediaStreamTrack.new(:audio)

codec = %RTPCodecParameters{
payload_type: 111,
mime_type: "audio/opus",
clock_rate: 48_000,
channels: 2,
sdp_fmtp_line: %FMTP{pt: 111, minptime: 10, useinbandfec: true}
@rtp_hdr_exts [%Extmap{id: 1, uri: "urn:ietf:params:rtp-hdrext:sdes:mid"}]

@codec %RTPCodecParameters{
payload_type: 111,
mime_type: "audio/opus",
clock_rate: 48_000,
channels: 2,
sdp_fmtp_line: %FMTP{pt: 111, minptime: 10, useinbandfec: true}
}

@rtx_codec %ExWebRTC.RTPCodecParameters{
payload_type: 124,
mime_type: "video/rtx",
clock_rate: 90000,

Check warning on line 25 in test/ex_webrtc/rtp_sender_test.exs

View workflow job for this annotation

GitHub Actions / CI on OTP 27 / Elixir 1.17

Numbers larger than 9999 should be written with underscores: 90_000
sdp_fmtp_line: %ExSDP.Attribute.FMTP{
pt: 124,
apt: 111
}
}

rtp_hdr_exts = [%Extmap{id: 1, uri: "urn:ietf:params:rtp-hdrext:sdes:mid"}]
setup do
track = MediaStreamTrack.new(:audio)

sender = RTPSender.new(track, codec, nil, rtp_hdr_exts, "1", @ssrc, @rtx_ssrc, [])
sender = RTPSender.new(track, @codec, nil, @rtp_hdr_exts, "1", @ssrc, @rtx_ssrc, [])

%{sender: sender}
end
Expand Down Expand Up @@ -53,6 +63,66 @@ defmodule ExWebRTC.RTPSenderTest do
assert packet.marker == true
end

describe "get_mline_attrs/1" do
test "without rtx" do
stream_id = MediaStreamTrack.generate_stream_id()
track = MediaStreamTrack.new(:audio, [stream_id])

sender = RTPSender.new(track, @codec, nil, @rtp_hdr_exts, "1", @ssrc, @rtx_ssrc, [])

assert [
%ExSDP.Attribute.MSID{id: ^stream_id, app_data: nil},
%ExSDP.Attribute.SSRC{id: @ssrc, attribute: "msid", value: ^stream_id}
] = RTPSender.get_mline_attrs(sender)
end

test "with rtx" do
stream_id = MediaStreamTrack.generate_stream_id()
track = MediaStreamTrack.new(:audio, [stream_id])

sender = RTPSender.new(track, @codec, @rtx_codec, @rtp_hdr_exts, "1", @ssrc, @rtx_ssrc, [])

assert [
%ExSDP.Attribute.MSID{id: ^stream_id, app_data: nil},
%ExSDP.Attribute.SSRCGroup{semantics: "FID", ssrcs: [@ssrc, @rtx_ssrc]},
%ExSDP.Attribute.SSRC{id: @ssrc, attribute: "msid", value: ^stream_id},
%ExSDP.Attribute.SSRC{id: @rtx_ssrc, attribute: "msid", value: ^stream_id}
] = RTPSender.get_mline_attrs(sender)
end

test "without media stream" do
track = MediaStreamTrack.new(:audio)

sender = RTPSender.new(track, @codec, @rtx_codec, @rtp_hdr_exts, "1", @ssrc, @rtx_ssrc, [])

assert [
%ExSDP.Attribute.MSID{id: "-", app_data: nil},
%ExSDP.Attribute.SSRCGroup{semantics: "FID", ssrcs: [@ssrc, @rtx_ssrc]},
%ExSDP.Attribute.SSRC{id: @ssrc, attribute: "msid", value: "-"},
%ExSDP.Attribute.SSRC{id: @rtx_ssrc, attribute: "msid", value: "-"}
] = RTPSender.get_mline_attrs(sender)
end

test "with multiple media streams" do
s1_id = MediaStreamTrack.generate_stream_id()
s2_id = MediaStreamTrack.generate_stream_id()

track = MediaStreamTrack.new(:audio, [s1_id, s2_id])

sender = RTPSender.new(track, @codec, @rtx_codec, @rtp_hdr_exts, "1", @ssrc, @rtx_ssrc, [])

assert [
%ExSDP.Attribute.MSID{id: ^s1_id, app_data: nil},
%ExSDP.Attribute.MSID{id: ^s2_id, app_data: nil},
%ExSDP.Attribute.SSRCGroup{semantics: "FID", ssrcs: [@ssrc, @rtx_ssrc]},
%ExSDP.Attribute.SSRC{id: @ssrc, attribute: "msid", value: ^s1_id},
%ExSDP.Attribute.SSRC{id: @ssrc, attribute: "msid", value: ^s2_id},
%ExSDP.Attribute.SSRC{id: @rtx_ssrc, attribute: "msid", value: ^s1_id},
%ExSDP.Attribute.SSRC{id: @rtx_ssrc, attribute: "msid", value: ^s2_id}
] = RTPSender.get_mline_attrs(sender)
end
end

test "get_stats/2", %{sender: sender} do
timestamp = System.os_time(:millisecond)
payload = <<1, 2, 3>>
Expand Down

0 comments on commit a644896

Please sign in to comment.