Skip to content
This repository was archived by the owner on Sep 19, 2024. It is now read-only.

Add medium encoding and expose FIR interval in WebRTC Endpoint options #96

Merged
merged 2 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions assets/js/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ export const simulcastConfig: RTCRtpTransceiverInit = {
scaleResolutionDownBy: 4.0,
// scalabilityMode: "L1T" + TEMPORAL_LAYERS_COUNT,
},
{
rid: "m",
active: true,
scaleResolutionDownBy: 2.0,
},
{
rid: "h",
active: true,
Expand Down
6 changes: 3 additions & 3 deletions assets/js/membraneWebRTC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,9 @@ export class MembraneWebRTC {
* receive in {@link onPeerJoined}. E.g. this can source of the track - wheather it's
* screensharing, webcam or some other media device.
* @param isSimulcast - Defines whether track should be simulcasted or not.
* At the moment simulcast track is sent in two versions - low and high.
* High resolution is the original track resolution while the low resolution
* is the original track resoultion scaled down by 4.
* At the moment simulcast track is sent in three versions - low, medium and high.
* High resolution is the original track resolution, while medium and low resolutions
* are the original track resoultion scaled down by 2 and 4 respectively.
* Those settings are not configurable at the moment.
* @returns {string} Returns id of added track
* @example
Expand Down
22 changes: 19 additions & 3 deletions lib/membrane_rtc_engine/endpoints/webrtc/forwarder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ defmodule Membrane.RTC.Engine.Endpoint.WebRTC.Forwarder do
:old_encoding,
selected_encoding: "h",
vp8_munger: VP8Munger.new(),
active_encodings: ["h", "l"],
active_encodings: ["h", "m", "l"],
started?: false
]

Expand Down Expand Up @@ -67,7 +67,7 @@ defmodule Membrane.RTC.Engine.Endpoint.WebRTC.Forwarder do
active_encodings: List.delete(forwarder.active_encodings, encoding)
}

do_select_encoding(forwarder, List.first(forwarder.active_encodings))
do_select_encoding(forwarder, get_next_encoding(forwarder.active_encodings))
end

def encoding_inactive(forwarder, encoding) do
Expand Down Expand Up @@ -101,7 +101,7 @@ defmodule Membrane.RTC.Engine.Endpoint.WebRTC.Forwarder do

# if we don't have any active encoding
forwarder.selected_encoding == nil ->
do_select_encoding(forwarder, List.first(forwarder.active_encodings))
do_select_encoding(forwarder, get_next_encoding(forwarder.active_encodings))

true ->
forwarder
Expand Down Expand Up @@ -146,6 +146,22 @@ defmodule Membrane.RTC.Engine.Endpoint.WebRTC.Forwarder do
%__MODULE__{forwarder | queued_encoding: encoding}
end

defp get_next_encoding(encodings) do
encodings |> sort_encodings() |> List.first()
end

defp sort_encodings(encodings) do
get_value = fn encoding ->
case encoding do
"h" -> 3
"m" -> 2
"l" -> 1
end
end

Enum.sort(encodings, fn e1, e2 -> get_value.(e1) > get_value.(e2) end)
end

@spec process(t(), Membrane.Buffer.t(), Endpoint.WebRTC.encoding_t(), any()) ::
{t(), [Membrane.Element.Action.t()]}
def process(%__MODULE__{started?: false} = forwarder, buffer, encoding, endpoint_id) do
Expand Down
6 changes: 5 additions & 1 deletion lib/membrane_rtc_engine/endpoints/webrtc/simulcast_tee.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ defmodule Membrane.RTC.Engine.Endpoint.WebRTC.SimulcastTee do
%{
clock_rate: opts.clock_rate,
forwarders: %{},
trackers: %{"l" => EncodingTracker.new("l"), "h" => EncodingTracker.new("h")},
trackers: %{
"l" => EncodingTracker.new("l"),
"m" => EncodingTracker.new("m"),
"h" => EncodingTracker.new("h")
},
update?: false
}}
end
Expand Down
14 changes: 12 additions & 2 deletions lib/membrane_rtc_engine/endpoints/webrtc_endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ if Code.ensure_loaded?(Membrane.WebRTC.EndpointBin) do
default: nil,
description:
"Sender reports's generation interval, set to nil to avoid reports generation"
],
rtcp_fir_interval: [
spec: Membrane.Time.t() | nil,
default: Membrane.Time.second(),
description: """
Defines how often FIR should be sent.

For more information refer to RFC 5104 section 4.3.1.
"""
]

def_input_pad :input,
Expand Down Expand Up @@ -183,7 +192,8 @@ if Code.ensure_loaded?(Membrane.WebRTC.EndpointBin) do
integrated_turn_options: opts.integrated_turn_options,
integrated_turn_domain: opts.integrated_turn_domain,
owner: opts.owner,
video_tracks_limit: opts.video_tracks_limit
video_tracks_limit: opts.video_tracks_limit,
rtcp_fir_interval: opts.rtcp_fir_interval
}

{{:ok, spec: spec, log_metadata: opts.log_metadata}, state}
Expand Down Expand Up @@ -350,7 +360,7 @@ if Code.ensure_loaded?(Membrane.WebRTC.EndpointBin) do
options: [
extensions: extensions,
use_depayloader?: false,
rtcp_fir_interval: Membrane.Time.seconds(5)
rtcp_fir_interval: state.rtcp_fir_interval
]
)
|> to_bin_output(pad)
Expand Down
6 changes: 3 additions & 3 deletions test/membrane_rtc_engine/webrtc/forwarder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ defmodule Membrane.RTC.Engine.Endpoint.WebRTC.ForwarderTest do

assert %Forwarder{
selected_encoding: "h",
queued_encoding: "l",
queued_encoding: "m",
old_encoding: "h",
active_encodings: ["l"]
active_encodings: ["m", "l"]
} = forwarder

forwarder = Forwarder.encoding_active(forwarder, "h")
Expand All @@ -20,7 +20,7 @@ defmodule Membrane.RTC.Engine.Endpoint.WebRTC.ForwarderTest do
selected_encoding: "h",
queued_encoding: nil,
old_encoding: nil,
active_encodings: ["l", "h"]
active_encodings: ["m", "l", "h"]
} = forwarder
end
end