Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RTC-333] Limit HLS components to 1 per room #72

Merged
merged 2 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 29 additions & 10 deletions lib/jellyfish/room.ex
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ defmodule Jellyfish.Room do
end

@spec add_component(id(), Component.component(), map()) ::
{:ok, Component.t()} | :error | {:error, :incompatible_codec}
{:ok, Component.t()}
| :error
| {:error, :incompatible_codec | :reached_components_limit}
def add_component(room_id, component_type, options \\ %{}) do
GenServer.call(registry_id(room_id), {:add_component, component_type, options})
end
Expand Down Expand Up @@ -216,18 +218,14 @@ defmodule Jellyfish.Room do
end

@impl true
def handle_call(
{:add_component, component_type, options},
_from,
%{config: %{video_codec: video_codec}} = state
) do
def handle_call({:add_component, component_type, options}, _from, state) do
options =
Map.merge(
%{engine_pid: state.engine_pid, room_id: state.id},
options
)

with :ok <- check_video_codec(video_codec, component_type),
with :ok <- check_component_allowed(component_type, state),
{:ok, component} <- Component.new(component_type, options) do
state = put_in(state, [:components, component.id], component)

Expand All @@ -241,6 +239,13 @@ defmodule Jellyfish.Room do
Logger.warn("Unable to add component: incompatible codec, HLS needs 'h264' video codec.")
{:reply, {:error, :incompatible_codec}, state}

{:error, :reached_components_limit} ->
Logger.warn(
"Unable to add component: reached components limit, max 1 HLS component allowed per room."
)

{:reply, {:error, :reached_components_limit}, state}

{:error, reason} ->
Logger.warn("Unable to add component: #{inspect(reason)}")
{:reply, :error, state}
Expand Down Expand Up @@ -386,7 +391,21 @@ defmodule Jellyfish.Room do

defp registry_id(room_id), do: {:via, Registry, {Jellyfish.RoomRegistry, room_id}}

defp check_video_codec(:h264, Jellyfish.Component.HLS), do: :ok
defp check_video_codec(_codec, Jellyfish.Component.HLS), do: {:error, :incompatible_codec}
defp check_video_codec(_codec, _component), do: :ok
defp check_component_allowed(Component.HLS, %{
config: %{video_codec: video_codec},
components: components
}) do
cond do
video_codec != :h264 ->
{:error, :incompatible_codec}

Map.values(components) |> Enum.any?(&(&1.type == Component.HLS)) ->
{:error, :reached_components_limit}

true ->
:ok
end
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about:

Suggested change
defp check_component_allowed(Component.HLS, %{
config: %{video_codec: video_codec},
components: components
}) do
cond do
video_codec != :h264 ->
{:error, :incompatible_codec}
Map.values(components) |> Enum.any?(&(&1.type == Component.HLS)) ->
{:error, :reached_components_limit}
true ->
:ok
end
end
defp check_component_allowed(Component.HLS, %{config: %{video_codec: video_codec}, components: components}) do
hls_components_present = components_includes_HLS?(components)
case {video_codec, hls_components_present} do
{:h264, false} -> :ok
{:h264, true} -> {:error, :reached_components_limit}
_ -> {:error, :incompatible_codec}
end
end
defp components_includes_HLS?(components) do
components
|> Map.values()
|> Enum.any?(&(&1.type == Component.HLS))
end


defp check_component_allowed(_component_type, _state), do: :ok
end
3 changes: 3 additions & 0 deletions lib/jellyfish_web/controllers/component_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ defmodule JellyfishWeb.ComponentController do

{:error, :incompatible_codec} ->
{:error, :bad_request, "HLS component needs room with video codec 'h264' enforced"}

{:error, :reached_components_limit} ->
{:error, :bad_request, "Max 1 HLS component allowed per room"}
end
end

Expand Down
8 changes: 7 additions & 1 deletion test/jellyfish_web/controllers/component_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ defmodule JellyfishWeb.ComponentControllerTest do
end

describe "create hls component" do
test "renders component when data is valid", %{conn: conn} do
test "renders component when data is valid, allows max 1 hls per room", %{conn: conn} do
room_conn = post(conn, ~p"/room", videoCodec: "h264")
assert %{"id" => room_id} = json_response(room_conn, :created)["data"]

Expand All @@ -57,6 +57,12 @@ defmodule JellyfishWeb.ComponentControllerTest do
]
} = json_response(conn, :ok)["data"]

# Try to add another hls component
conn = post(conn, ~p"/room/#{room_id}/component", type: "hls")

assert json_response(conn, :bad_request)["errors"] ==
"Max 1 HLS component allowed per room"

room_conn = delete(conn, ~p"/room/#{room_id}")
assert response(room_conn, :no_content)
end
Expand Down