Skip to content

Commit

Permalink
Add rids field to MediaStreamTrack
Browse files Browse the repository at this point in the history
  • Loading branch information
LVala committed Jun 24, 2024
1 parent c65e1cb commit 41ff049
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
17 changes: 14 additions & 3 deletions lib/ex_webrtc/media_stream_track.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,37 @@ defmodule ExWebRTC.MediaStreamTrack do
Mimics [MediaStreamTrack](https://www.w3.org/TR/mediacapture-streams/#dom-mediastreamtrack).
"""

alias ExWebRTC.SDPUtils
alias ExWebRTC.Utils

@type id() :: integer()
@type stream_id() :: String.t()
@type rid() :: String.t()
@type kind() :: :audio | :video

@type t() :: %__MODULE__{
kind: kind(),
id: id(),
streams: [stream_id()]
streams: [stream_id()],
rids: [rid()] | nil
}

@enforce_keys [:id, :kind]
defstruct @enforce_keys ++ [streams: []]
defstruct @enforce_keys ++ [streams: [], rids: nil]

@spec new(kind()) :: t()
@spec new(kind(), [stream_id()]) :: t()
def new(kind, streams \\ []) when kind in [:audio, :video] do
%__MODULE__{kind: kind, id: Utils.generate_id(), streams: streams}
end

@doc false
@spec from_mline(ExSDP.Media.t()) :: t()
def from_mline(mline) do
streams = SDPUtils.get_stream_ids(mline)
rids = SDPUtils.get_rids(mline)
%__MODULE__{kind: mline.type, id: Utils.generate_id(), streams: streams, rids: rids}
end

@spec generate_stream_id() :: stream_id()
def generate_stream_id() do
20
Expand Down
3 changes: 1 addition & 2 deletions lib/ex_webrtc/rtp_transceiver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ defmodule ExWebRTC.RTPTransceiver do
rtp_hdr_exts = get_rtp_hdr_extensions(mline, config)
{:mid, mid} = ExSDP.get_attribute(mline, :mid)

stream_ids = SDPUtils.get_stream_ids(mline)
track = MediaStreamTrack.new(mline.type, stream_ids)
track = MediaStreamTrack.from_mline(mline)
codec = get_codec(codecs)
rtx_codec = get_rtx(codecs, codec)

Expand Down
12 changes: 12 additions & 0 deletions lib/ex_webrtc/sdp_utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ defmodule ExWebRTC.SDPUtils do
end)
end

@spec get_rids(ExSDP.Media.t()) :: [String.t()] | nil
def get_rids(media) do
Enum.flat_map(media.attributes, fn
%RID{direction: :send, id: id} -> [id]
_other -> []
end)
|> case do
[] -> nil
other -> other
end
end

@spec reverse_simulcast(ExSDP.Media.t()) :: [ExSDP.Attribute.t()]
def reverse_simulcast(media) do
Enum.flat_map(media.attributes, fn
Expand Down
3 changes: 2 additions & 1 deletion test/ex_webrtc/peer_connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,8 @@ defmodule ExWebRTC.PeerConnectionTest do
assert %ExSDP.Attribute.Extmap{id: twcc_id} =
Enum.find(transceiver.rtp_hdr_exts, &(&1.uri == @twcc_uri))

assert_receive {:ex_webrtc, ^pc2, {:track, %MediaStreamTrack{kind: :video, id: id2}}}
assert_receive {:ex_webrtc, ^pc2,
{:track, %MediaStreamTrack{kind: :video, id: id2, rids: ^rids}}}

rids
|> Enum.with_index()
Expand Down

0 comments on commit 41ff049

Please sign in to comment.