Skip to content

Commit e47e165

Browse files
committed
Add track to MSID
1 parent 5e20c3d commit e47e165

File tree

2 files changed

+78
-42
lines changed

2 files changed

+78
-42
lines changed

lib/ex_webrtc/rtp_transceiver.ex

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -578,16 +578,20 @@ defmodule ExWebRTC.RTPTransceiver do
578578

579579
@doc false
580580
defp get_sender_attrs(track, codecs, ssrc, rtx_ssrc) do
581-
# Don't include track id. See RFC 8829 sec. 5.2.1
581+
# According to RFC 8829 sec. 5.2.1, track IDs should not be included.
582+
# However, most browsers support track IDs in MSID. We will follow this practice.
582583
msid_attrs =
583584
case track do
584-
%MediaStreamTrack{streams: streams} when streams != [] ->
585-
Enum.map(streams, &ExSDP.Attribute.MSID.new(&1, nil))
585+
%MediaStreamTrack{streams: streams, id: id} when streams != [] ->
586+
Enum.map(streams, &ExSDP.Attribute.MSID.new(&1, id))
586587

587-
_other ->
588+
%MediaStreamTrack{id: id} ->
588589
# In theory, we should do this "for each MediaStream that was associated with the transceiver",
589590
# but web browsers (chrome, ff) include MSID even when there aren't any MediaStreams
590-
[ExSDP.Attribute.MSID.new("-", nil)]
591+
[ExSDP.Attribute.MSID.new("-", id)]
592+
593+
nil ->
594+
[]
591595
end
592596

593597
ssrc_attrs = get_ssrc_attrs(codecs, ssrc, rtx_ssrc, track)
@@ -609,43 +613,38 @@ defmodule ExWebRTC.RTPTransceiver do
609613

610614
# we have a codec but not rtx codec
611615
defp do_get_ssrc_attrs(_codec, false, ssrc, _rtx_ssrc, track) do
612-
streams = (track && track.streams) || []
613-
614-
case streams do
615-
[] ->
616-
[%ExSDP.Attribute.SSRC{id: ssrc, attribute: "msid", value: "-"}]
617-
618-
streams ->
616+
case track do
617+
%MediaStreamTrack{streams: streams, id: id} when streams != [] ->
619618
Enum.map(streams, fn stream ->
620-
%ExSDP.Attribute.SSRC{id: ssrc, attribute: "msid", value: stream}
619+
%ExSDP.Attribute.SSRC{id: ssrc, attribute: "msid", value: "#{stream} #{id}"}
621620
end)
621+
622+
%MediaStreamTrack{id: id} ->
623+
[%ExSDP.Attribute.SSRC{id: ssrc, attribute: "msid", value: "- #{id}"}]
624+
625+
nil ->
626+
[%ExSDP.Attribute.SSRC{id: ssrc, attribute: "msid", value: "- -"}]
622627
end
623628
end
624629

625630
# we have both codec and rtx codec
626631
defp do_get_ssrc_attrs(_codec, _rtx_codec, ssrc, rtx_ssrc, track) do
627-
streams = (track && track.streams) || []
628-
629632
fid = %ExSDP.Attribute.SSRCGroup{semantics: "FID", ssrcs: [ssrc, rtx_ssrc]}
630633

631634
ssrc_attrs =
632-
case streams do
633-
[] ->
634-
[
635-
%ExSDP.Attribute.SSRC{id: ssrc, attribute: "msid", value: "-"},
636-
%ExSDP.Attribute.SSRC{id: rtx_ssrc, attribute: "msid", value: "-"}
637-
]
638-
639-
streams ->
635+
case track do
636+
%MediaStreamTrack{streams: streams, id: id} when streams != [] ->
640637
{ssrc_attrs, rtx_ssrc_attrs} =
641638
Enum.reduce(streams, {[], []}, fn stream, {ssrc_attrs, rtx_ssrc_attrs} ->
642-
ssrc_attr = %ExSDP.Attribute.SSRC{id: ssrc, attribute: "msid", value: stream}
639+
ssrc_value = "#{stream} #{id}"
640+
641+
ssrc_attr = %ExSDP.Attribute.SSRC{id: ssrc, attribute: "msid", value: ssrc_value}
643642
ssrc_attrs = [ssrc_attr | ssrc_attrs]
644643

645644
rtx_ssrc_attr = %ExSDP.Attribute.SSRC{
646645
id: rtx_ssrc,
647646
attribute: "msid",
648-
value: stream
647+
value: ssrc_value
649648
}
650649

651650
rtx_ssrc_attrs = [rtx_ssrc_attr | rtx_ssrc_attrs]
@@ -654,6 +653,18 @@ defmodule ExWebRTC.RTPTransceiver do
654653
end)
655654

656655
Enum.reverse(ssrc_attrs) ++ Enum.reverse(rtx_ssrc_attrs)
656+
657+
%MediaStreamTrack{id: id} ->
658+
[
659+
%ExSDP.Attribute.SSRC{id: ssrc, attribute: "msid", value: "- #{id}"},
660+
%ExSDP.Attribute.SSRC{id: rtx_ssrc, attribute: "msid", value: "- #{id}"}
661+
]
662+
663+
nil ->
664+
[
665+
%ExSDP.Attribute.SSRC{id: ssrc, attribute: "msid", value: "- -"},
666+
%ExSDP.Attribute.SSRC{id: rtx_ssrc, attribute: "msid", value: "- -"}
667+
]
657668
end
658669

659670
[fid | ssrc_attrs]

test/ex_webrtc/rtp_transceiver_test.exs

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,14 @@ defmodule ExWebRTC.RTPTransceiverTest do
7474
)
7575

7676
mline = RTPTransceiver.to_offer_mline(tr, @opts)
77+
ssrc_value = ssrc_value(@stream_id, @track.id)
7778

7879
assert [%ExSDP.Attribute.MSID{id: @stream_id}] =
7980
ExSDP.get_attributes(mline, ExSDP.Attribute.MSID)
8081

8182
assert [] = ExSDP.get_attributes(mline, ExSDP.Attribute.SSRCGroup)
8283

83-
assert [%ExSDP.Attribute.SSRC{id: @ssrc, attribute: "msid", value: @stream_id}] =
84+
assert [%ExSDP.Attribute.SSRC{id: @ssrc, attribute: "msid", value: ^ssrc_value}] =
8485
ExSDP.get_attributes(mline, ExSDP.Attribute.SSRC)
8586
end
8687

@@ -93,16 +94,17 @@ defmodule ExWebRTC.RTPTransceiverTest do
9394
)
9495

9596
mline = RTPTransceiver.to_offer_mline(tr, @opts)
97+
ssrc_value = ssrc_value(@stream_id, @track.id)
9698

97-
assert [%ExSDP.Attribute.MSID{id: @stream_id, app_data: nil}] =
99+
assert [%ExSDP.Attribute.MSID{id: @stream_id, app_data: @track.id}] ==
98100
ExSDP.get_attributes(mline, ExSDP.Attribute.MSID)
99101

100102
assert [%ExSDP.Attribute.SSRCGroup{semantics: "FID", ssrcs: [@ssrc, @rtx_ssrc]}] =
101103
ExSDP.get_attributes(mline, ExSDP.Attribute.SSRCGroup)
102104

103105
assert [
104-
%ExSDP.Attribute.SSRC{id: @ssrc, attribute: "msid", value: @stream_id},
105-
%ExSDP.Attribute.SSRC{id: @rtx_ssrc, attribute: "msid", value: @stream_id}
106+
%ExSDP.Attribute.SSRC{id: @ssrc, attribute: "msid", value: ^ssrc_value},
107+
%ExSDP.Attribute.SSRC{id: @rtx_ssrc, attribute: "msid", value: ^ssrc_value}
106108
] = ExSDP.get_attributes(mline, ExSDP.Attribute.SSRC)
107109
end
108110

@@ -118,16 +120,16 @@ defmodule ExWebRTC.RTPTransceiverTest do
118120

119121
mline = RTPTransceiver.to_offer_mline(tr, @opts)
120122

121-
assert [%ExSDP.Attribute.MSID{id: "-", app_data: nil}] =
123+
assert [%ExSDP.Attribute.MSID{id: "-", app_data: track.id}] ==
122124
ExSDP.get_attributes(mline, ExSDP.Attribute.MSID)
123125

124126
assert [%ExSDP.Attribute.SSRCGroup{semantics: "FID", ssrcs: [@ssrc, @rtx_ssrc]}] =
125127
ExSDP.get_attributes(mline, ExSDP.Attribute.SSRCGroup)
126128

127129
assert [
128-
%ExSDP.Attribute.SSRC{id: @ssrc, attribute: "msid", value: "-"},
129-
%ExSDP.Attribute.SSRC{id: @rtx_ssrc, attribute: "msid", value: "-"}
130-
] = ExSDP.get_attributes(mline, ExSDP.Attribute.SSRC)
130+
%ExSDP.Attribute.SSRC{id: @ssrc, attribute: "msid", value: "- #{track.id}"},
131+
%ExSDP.Attribute.SSRC{id: @rtx_ssrc, attribute: "msid", value: "- #{track.id}"}
132+
] == ExSDP.get_attributes(mline, ExSDP.Attribute.SSRC)
131133
end
132134

133135
test "with multiple media streams" do
@@ -145,20 +147,23 @@ defmodule ExWebRTC.RTPTransceiverTest do
145147

146148
mline = RTPTransceiver.to_offer_mline(tr, @opts)
147149

150+
ssrc1_value = ssrc_value(s1_id, track.id)
151+
ssrc2_value = ssrc_value(s2_id, track.id)
152+
148153
assert [
149-
%ExSDP.Attribute.MSID{id: ^s1_id, app_data: nil},
150-
%ExSDP.Attribute.MSID{id: ^s2_id, app_data: nil}
151-
] = ExSDP.get_attributes(mline, ExSDP.Attribute.MSID)
154+
%ExSDP.Attribute.MSID{id: s1_id, app_data: track.id},
155+
%ExSDP.Attribute.MSID{id: s2_id, app_data: track.id}
156+
] == ExSDP.get_attributes(mline, ExSDP.Attribute.MSID)
152157

153158
assert [%ExSDP.Attribute.SSRCGroup{semantics: "FID", ssrcs: [@ssrc, @rtx_ssrc]}] =
154159
ExSDP.get_attributes(mline, ExSDP.Attribute.SSRCGroup)
155160

156161
assert [
157-
%ExSDP.Attribute.SSRC{id: @ssrc, attribute: "msid", value: ^s1_id},
158-
%ExSDP.Attribute.SSRC{id: @ssrc, attribute: "msid", value: ^s2_id},
159-
%ExSDP.Attribute.SSRC{id: @rtx_ssrc, attribute: "msid", value: ^s1_id},
160-
%ExSDP.Attribute.SSRC{id: @rtx_ssrc, attribute: "msid", value: ^s2_id}
161-
] = ExSDP.get_attributes(mline, ExSDP.Attribute.SSRC)
162+
%ExSDP.Attribute.SSRC{id: @ssrc, attribute: "msid", value: ssrc1_value},
163+
%ExSDP.Attribute.SSRC{id: @ssrc, attribute: "msid", value: ssrc2_value},
164+
%ExSDP.Attribute.SSRC{id: @rtx_ssrc, attribute: "msid", value: ssrc1_value},
165+
%ExSDP.Attribute.SSRC{id: @rtx_ssrc, attribute: "msid", value: ssrc2_value}
166+
] == ExSDP.get_attributes(mline, ExSDP.Attribute.SSRC)
162167
end
163168

164169
test "without codecs" do
@@ -174,12 +179,30 @@ defmodule ExWebRTC.RTPTransceiverTest do
174179

175180
mline = RTPTransceiver.to_offer_mline(tr, @opts)
176181

177-
assert [%ExSDP.Attribute.MSID{id: @stream_id, app_data: nil}] =
182+
assert [%ExSDP.Attribute.MSID{id: @stream_id, app_data: @track.id}] ==
178183
ExSDP.get_attributes(mline, ExSDP.Attribute.MSID)
179184

180185
assert [] = ExSDP.get_attributes(mline, ExSDP.Attribute.SSRCGroup)
181186
assert [] = ExSDP.get_attributes(mline, ExSDP.Attribute.SSRC)
182187
end
188+
189+
test "without track" do
190+
tr =
191+
RTPTransceiver.new(:video, nil, @config,
192+
ssrc: @ssrc,
193+
rtx_ssrc: @rtx_ssrc,
194+
direction: :sendrecv
195+
)
196+
197+
mline = RTPTransceiver.to_offer_mline(tr, @opts)
198+
199+
assert [] = ExSDP.get_attributes(mline, ExSDP.Attribute.MSID)
200+
201+
assert [
202+
%ExSDP.Attribute.SSRC{id: @ssrc, attribute: "msid", value: "- -"},
203+
%ExSDP.Attribute.SSRC{id: @rtx_ssrc, attribute: "msid", value: "- -"}
204+
] = ExSDP.get_attributes(mline, ExSDP.Attribute.SSRC)
205+
end
183206
end
184207

185208
defp test_sender_attrs_present(tr) do
@@ -201,4 +224,6 @@ defmodule ExWebRTC.RTPTransceiverTest do
201224
assert [] == ExSDP.get_attributes(mline, ExSDP.Attribute.SSRCGroup)
202225
assert [] == ExSDP.get_attributes(mline, ExSDP.Attribute.SSRC)
203226
end
227+
228+
defp ssrc_value(stream, app_data), do: "#{stream} #{app_data}"
204229
end

0 commit comments

Comments
 (0)