Skip to content
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
8 changes: 6 additions & 2 deletions lib/jellyfish/component/hls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ defmodule Jellyfish.Component.HLS do
low_latency: false,
persistent: false,
target_window_duration: nil,
subscribe_mode: :auto,
s3: nil
]

@type t :: %__MODULE__{
low_latency: boolean(),
persistent: boolean(),
target_window_duration: pos_integer() | nil,
subscribe_mode: :auto | :manual,
s3: credentials() | nil
}

Expand All @@ -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
23 changes: 19 additions & 4 deletions lib/jellyfish/room.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -258,18 +259,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 @subscribe_modes, 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)
Expand Down
20 changes: 19 additions & 1 deletion test/jellyfish/room_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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 %{
Expand Down Expand Up @@ -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{})
Expand Down