From d84c513e554fd94d90642f75ac85894cd6d6206c Mon Sep 17 00:00:00 2001 From: KarolK99 Date: Tue, 21 Nov 2023 18:15:50 +0100 Subject: [PATCH 1/2] Add hls subscribe mode to API --- lib/jellyfish/component/hls.ex | 8 ++++++-- lib/jellyfish/room.ex | 22 ++++++++++++++++++---- test/jellyfish/room_test.exs | 20 +++++++++++++++++++- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/lib/jellyfish/component/hls.ex b/lib/jellyfish/component/hls.ex index 855832c..88497ce 100644 --- a/lib/jellyfish/component/hls.ex +++ b/lib/jellyfish/component/hls.ex @@ -21,6 +21,7 @@ defmodule Jellyfish.Component.HLS do low_latency: false, persistent: false, target_window_duration: nil, + subscribe_mode: :auto, s3: nil ] @@ -28,6 +29,7 @@ defmodule Jellyfish.Component.HLS do low_latency: boolean(), persistent: boolean(), target_window_duration: pos_integer() | nil, + subscribe_mode: :auto | :manual, s3: credentials() | nil } @@ -36,13 +38,15 @@ defmodule Jellyfish.Component.HLS do "playable" => playable, "lowLatency" => low_latency, "persistent" => persistent, - "targetWindowDuration" => target_window_duration + "targetWindowDuration" => target_window_duration, + "subscribeMode" => subscribe_mode }) do %{ playable: playable, low_latency: low_latency, persistent: persistent, - target_window_duration: target_window_duration + target_window_duration: target_window_duration, + subscribe_mode: subscribe_mode } end end diff --git a/lib/jellyfish/room.ex b/lib/jellyfish/room.ex index a1c52b8..553a8ff 100644 --- a/lib/jellyfish/room.ex +++ b/lib/jellyfish/room.ex @@ -258,18 +258,32 @@ defmodule Jellyfish.Room do end defp validate_component(%RTSP{}), do: :ok - defp validate_component(%HLS{s3: nil}), do: :ok - defp validate_component(%HLS{s3: %{} = s3}), do: validate_s3_credentials(s3) + + defp validate_component(%HLS{s3: s3, subscribe_mode: subscribe_mode}) do + with :ok <- validate_s3_credentials(s3), + :ok <- validate_subscribe_mode(subscribe_mode) do + :ok + else + :error -> {:error, :component_validation} + end + end + defp validate_component(_component), do: {:error, :component_validation} - defp validate_s3_credentials(credentials) do + defp validate_s3_credentials(%{} = credentials) do keys = Map.keys(credentials) if @s3_keys -- keys == [] and keys -- @s3_keys == [], do: :ok, - else: {:error, :component_validation} + else: :error end + defp validate_s3_credentials(nil), do: :ok + defp validate_s3_credentials(_credentials), do: :error + + defp validate_subscribe_mode(mode) when mode in [:auto, :manual], do: :ok + defp validate_subscribe_mode(_mode), do: :error + defp map_snake_case_to_camel_case(%{} = map), do: Map.new(map, fn {k, v} -> {snake_case_to_camel_case(k), map_snake_case_to_camel_case(v)} end) diff --git a/test/jellyfish/room_test.exs b/test/jellyfish/room_test.exs index 695b435..bf06a61 100644 --- a/test/jellyfish/room_test.exs +++ b/test/jellyfish/room_test.exs @@ -12,7 +12,8 @@ defmodule Jellyfish.RoomTest do playable: false, low_latency: false, persistent: false, - target_window_duration: nil + target_window_duration: nil, + subscribe_mode: "auto" } @s3 %{ @@ -184,6 +185,23 @@ defmodule Jellyfish.RoomTest do end end + test "when request is valid with manual subscribe mode", %{client: client, room_id: room_id} do + assert {:ok, component} = + Room.add_component(client, room_id, %{ + @hls_component_opts + | subscribe_mode: :manual + }) + + hls_metadata = %{@hls_metadata | subscribe_mode: "manual"} + assert %Component{type: Component.HLS, metadata: ^hls_metadata} = component + end + + test "when request is invalid - wrong subscribe mode", %{client: client, room_id: room_id} do + assert_raise OptionsError, fn -> + Room.add_component(client, room_id, %{@hls_component_opts | subscribe_mode: :wrong_mode}) + end + end + test "when request is invalid", %{client: client} do assert_raise OptionsError, fn -> Room.add_component(client, @room_id, %InvalidComponentOpts{}) From db46374c5ed65afb509aa319e92ded2179955785 Mon Sep 17 00:00:00 2001 From: KarolK99 Date: Thu, 23 Nov 2023 12:30:13 +0100 Subject: [PATCH 2/2] Requested changes --- lib/jellyfish/room.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/jellyfish/room.ex b/lib/jellyfish/room.ex index 553a8ff..e78f347 100644 --- a/lib/jellyfish/room.ex +++ b/lib/jellyfish/room.ex @@ -36,6 +36,7 @@ defmodule Jellyfish.Room do alias Jellyfish.Exception.StructureError @s3_keys [:access_key_id, :secret_access_key, :region, :bucket] + @subscribe_modes [:auto, :manual] @enforce_keys [ :id, @@ -281,7 +282,7 @@ defmodule Jellyfish.Room do defp validate_s3_credentials(nil), do: :ok defp validate_s3_credentials(_credentials), do: :error - defp validate_subscribe_mode(mode) when mode in [:auto, :manual], do: :ok + defp validate_subscribe_mode(mode) when mode in @subscribe_modes, do: :ok defp validate_subscribe_mode(_mode), do: :error defp map_snake_case_to_camel_case(%{} = map),