Skip to content

Commit

Permalink
Restrict available codecs in room for recording component (#169)
Browse files Browse the repository at this point in the history
* Restrict available codecs in room for recording component

* Restrict recording component to one per room

* Requested changes

* Requested changes
  • Loading branch information
Karolk99 authored Apr 2, 2024
1 parent 3891613 commit 6d3b5a3
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 26 deletions.
12 changes: 12 additions & 0 deletions lib/jellyfish/component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ defmodule Jellyfish.Component do
end
end

@spec to_string!(module()) :: String.t()
def to_string!(component) do
case component do
HLS -> "hls"
RTSP -> "rtsp"
File -> "file"
SIP -> "sip"
Recording -> "recording"
_other -> raise "Invalid component"
end
end

@spec new(component(), map()) :: {:ok, t()} | {:error, term()}
def new(type, options) do
with {:ok, %{endpoint: endpoint, properties: properties}} <- type.config(options) do
Expand Down
22 changes: 12 additions & 10 deletions lib/jellyfish/room.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule Jellyfish.Room do
require Logger

alias Jellyfish.Component
alias Jellyfish.Component.{HLS, RTSP, SIP}
alias Jellyfish.Component.{HLS, Recording, RTSP, SIP}
alias Jellyfish.Event
alias Jellyfish.Peer
alias Jellyfish.Room.Config
Expand Down Expand Up @@ -281,9 +281,10 @@ defmodule Jellyfish.Room do
Logger.warning("Unable to add component: incompatible codec")
{:reply, {:error, :incompatible_codec}, state}

{:error, :reached_components_limit_hls} ->
Logger.warning("Unable to add component: reached components limit")
{:reply, {:error, :reached_components_limit_hls}, state}
{:error, :reached_components_limit} ->
type = Component.to_string!(component_type)
Logger.warning("Unable to add component: reached components limit #{type}")
{:reply, {:error, {:reached_components_limit, type}}, state}

{:error, :file_does_not_exist} ->
Logger.warning("Unable to add component: file does not exist")
Expand Down Expand Up @@ -771,16 +772,17 @@ defmodule Jellyfish.Room do
if component.type == HLS, do: component
end)

defp check_component_allowed(HLS, %{
defp check_component_allowed(type, %{
config: %{video_codec: video_codec},
components: components
}) do
})
when type in [HLS, Recording] do
cond do
video_codec != :h264 ->
{:error, :incompatible_codec}

hls_component_already_present?(components) ->
{:error, :reached_components_limit_hls}
component_already_present?(type, components) ->
{:error, :reached_components_limit}

true ->
:ok
Expand All @@ -797,8 +799,8 @@ defmodule Jellyfish.Room do

defp check_component_allowed(_component_type, _state), do: :ok

defp hls_component_already_present?(components),
do: components |> Map.values() |> Enum.any?(&(&1.type == HLS))
defp component_already_present?(type, components),
do: components |> Map.values() |> Enum.any?(&(&1.type == type))

defp validate_hls_subscription(nil), do: {:error, :hls_component_not_exists}

Expand Down
5 changes: 3 additions & 2 deletions lib/jellyfish_web/controllers/component_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ defmodule JellyfishWeb.ComponentController do
{:error, :unsupported_file_type} ->
{:error, :bad_request, "Unsupported file type"}

{:error, :reached_components_limit_hls} ->
{:error, :bad_request, "Reached components limit for component HLS in room #{room_id}"}
{:error, {:reached_components_limit, type}} ->
{:error, :bad_request,
"Reached #{type} components limit for component in room #{room_id}"}
end
end

Expand Down
15 changes: 1 addition & 14 deletions test/jellyfish_web/controllers/component/hls_component_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ defmodule JellyfishWeb.Component.HlsComponentTest do
conn = post(conn, ~p"/room/#{room_id}/component", type: "hls")

assert model_response(conn, :bad_request, "Error")["errors"] ==
"Reached components limit for component HLS in room #{room_id}"
"Reached hls components limit for component in room #{room_id}"

conn = delete(conn, ~p"/room/#{room_id}")
assert response(conn, :no_content)
Expand Down Expand Up @@ -208,19 +208,6 @@ defmodule JellyfishWeb.Component.HlsComponentTest do
end
end

defp create_h264_room(%{conn: conn}) do
conn = post(conn, ~p"/room", videoCodec: "h264")

assert %{"id" => room_id} =
model_response(conn, :created, "RoomCreateDetailsResponse")["data"]["room"]

on_exit(fn ->
RoomService.delete_room(room_id)
end)

%{room_id: room_id}
end

defp assert_hls_path(room_id, persistent: persistent) do
hls_path = HLS.output_dir(room_id, persistent: persistent)
assert {:ok, ^hls_path} = HLS.EtsHelper.get_hls_folder_path(room_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ defmodule JellyfishWeb.Component.RecordingComponentTest do

import Mox

alias Jellyfish.RoomService

@s3_credentials %{
accessKeyId: "access_key_id",
secretAccessKey: "secret_access_key",
Expand All @@ -14,6 +16,7 @@ defmodule JellyfishWeb.Component.RecordingComponentTest do
@path_prefix "path_prefix"

describe "create recording component" do
setup [:create_h264_room]
setup :set_mox_from_context

test "renders component with required options", %{conn: conn, room_id: room_id} do
Expand All @@ -34,6 +37,12 @@ defmodule JellyfishWeb.Component.RecordingComponentTest do
} = model_response(conn, :created, "ComponentDetailsResponse")

assert_component_created(conn, room_id, id, "recording")

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

assert model_response(conn, :bad_request, "Error")["errors"] ==
"Reached recording components limit for component in room #{room_id}"
end

setup :set_mox_from_context
Expand Down Expand Up @@ -74,6 +83,22 @@ defmodule JellyfishWeb.Component.RecordingComponentTest do
assert model_response(conn, :bad_request, "Error")["errors"] ==
"S3 credentials has to be passed either by request or at application startup as envs"
end

test "renders errors when video codec is different than h264 - vp8", %{conn: conn} do
Application.put_env(:jellyfish, :s3_credentials, @s3_credentials)
conn = post(conn, ~p"/room", videoCodec: "vp8")

assert %{"id" => room_id} =
model_response(conn, :created, "RoomCreateDetailsResponse")["data"]["room"]

conn = post(conn, ~p"/room/#{room_id}/component", type: "recording")

assert model_response(conn, :bad_request, "Error")["errors"] ==
"Incompatible video codec enforced in room #{room_id}"

RoomService.delete_room(room_id)
Application.put_env(:jellyfish, :s3_credentials, nil)
end
end

defp mock_http_request() do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ defmodule JellyfishWeb.ComponentControllerTest do
end

describe "delete component" do
setup [:create_h264_room]
setup [:create_rtsp_component]

test "deletes chosen component", %{conn: conn, room_id: room_id, component_id: component_id} do
Expand Down
16 changes: 16 additions & 0 deletions test/support/component_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ defmodule JellyfishWeb.ComponentCase do
use ExUnit.CaseTemplate
use JellyfishWeb.ConnCase

alias Jellyfish.RoomService

using do
quote do
import JellyfishWeb.ComponentCase
Expand Down Expand Up @@ -71,4 +73,18 @@ defmodule JellyfishWeb.ComponentCase do
JellyfishWeb.ApiSpec.spec()
)
end

@spec create_h264_room(context :: term()) :: map()
def create_h264_room(%{conn: conn}) do
conn = post(conn, ~p"/room", videoCodec: "h264")

assert %{"id" => room_id} =
model_response(conn, :created, "RoomCreateDetailsResponse")["data"]["room"]

on_exit(fn ->
RoomService.delete_room(room_id)
end)

%{room_id: room_id}
end
end

0 comments on commit 6d3b5a3

Please sign in to comment.