Skip to content

Commit

Permalink
RTC-510 Change hls_subscribe to subscribe (#61)
Browse files Browse the repository at this point in the history
* Change hls_subscribe to subscribe

* Update lib/jellyfish/room.ex

Co-authored-by: Karol Konkol <[email protected]>

* Update lib/jellyfish/utils.ex

Co-authored-by: Karol Konkol <[email protected]>

* Format code

* Adjustment to new jellyfish

---------

Co-authored-by: Karol Konkol <[email protected]>
  • Loading branch information
Rados13 and Karolk99 authored Apr 10, 2024
1 parent d8f253f commit d71c89d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 33 deletions.
9 changes: 5 additions & 4 deletions lib/jellyfish/component/recording.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ defmodule Jellyfish.Component.Recording do
alias Jellyfish.Component.HLS

@enforce_keys []
defstruct @enforce_keys ++ [credentials: nil, path_prefix: nil]
defstruct @enforce_keys ++ [credentials: nil, path_prefix: nil, subscribe_mode: :auto]

@type t :: %__MODULE__{
credentials: HLS.credentials() | nil,
path_prefix: Path.t() | nil
path_prefix: Path.t() | nil,
subscribe_mode: :manual | :auto
}

@impl true
def properties_from_json(%{"pathPrefix" => path_prefix}) do
%{path_prefix: path_prefix}
def properties_from_json(%{"subscribeMode" => subscribe_mode}) do
%{subscribe_mode: subscribe_mode}
end
end
21 changes: 14 additions & 7 deletions lib/jellyfish/room.ex
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ defmodule Jellyfish.Room do
%{
"type" => Component.string_from_options(component),
"options" =>
Map.from_struct(component)
component
|> Map.from_struct()
|> map_snake_case_to_camel_case()
}
),
Expand Down Expand Up @@ -235,19 +236,25 @@ defmodule Jellyfish.Room do
end

@doc """
Adds peers and components tracks to hls component
Adds peers and components tracks to hls or recording component
In order to subscribe to HLS peers/components, the HLS component should be initialized with the subscribe_mode set to :manual.
This mode proves beneficial when you do not wish to record or stream all the available streams within a room via HLS.
In order to subscribe the component to peers/components, the component should be initialized with the subscribe_mode set to :manual.
This mode proves beneficial when you do not wish to record or stream all the available streams within a room.
It allows for selective addition instead – you can manually select specific streams.
For instance, you could opt to record only the stream of an event's host.
"""
@spec hls_subscribe(Client.t(), id(), [Peer.id() | Component.id()]) ::
@spec subscribe(Client.t(), id(), Component.id(), [Peer.id() | Component.id()]) ::
:ok | {:error, atom() | String.t()}
def hls_subscribe(client, room_id, origins) do
def subscribe(client, room_id, component_id, origins) do
with :ok <- validate_origins(origins),
{:ok, %Env{status: 201}} <-
Tesla.post(client.http_client, "/hls/#{room_id}/subscribe", %{origins: origins}) do
Tesla.post(
client.http_client,
"/room/#{room_id}/component/#{component_id}/subscribe",
%{
origins: origins
}
) do
:ok
else
error -> Utils.handle_response_error(error)
Expand Down
6 changes: 5 additions & 1 deletion lib/jellyfish/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ defmodule Jellyfish.Utils do
@type error :: {:ok, %Env{}} | {:error, term()}

@spec handle_response_error(error()) :: {:error, term()}
def handle_response_error({:ok, %Env{body: %{"errors" => error}}}),
@spec handle_response_error(error()) :: {:error, term()}
def handle_response_error({:ok, %Env{body: %{"errors" => error}}}) when is_binary(error),
do: {:error, "Request failed: #{error}"}

def handle_response_error({:ok, %Env{body: %{"errors" => error}}}),
do: {:error, "Request failed: #{inspect(error)}"}

def handle_response_error({:ok, %Env{body: body}}), do: raise(StructureError, body)
def handle_response_error({:error, :component_validation}), do: raise(OptionsError)
def handle_response_error({:error, reason}), do: {:error, reason}
Expand Down
60 changes: 39 additions & 21 deletions test/jellyfish/room_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ defmodule Jellyfish.RoomTest do
path_prefix: "test"
}

@recording_properties %{path_prefix: "test"}
@recording_properties %{subscribe_mode: "auto"}

@sip_component_opts %Component.SIP{
registrar_credentials: %{
Expand Down Expand Up @@ -80,6 +80,7 @@ defmodule Jellyfish.RoomTest do
@video_codec :h264

@invalid_room_id "invalid_mock_room_id"
@invalid_component_id "invalid_mock_component_id"
@invalid_max_peers "abc"
@invalid_video_codec :opus

Expand Down Expand Up @@ -132,8 +133,7 @@ defmodule Jellyfish.RoomTest do
test "invalid token" do
client = Client.new(server_api_token: "invalid" <> @server_api_token)

assert {:error, _reason} =
Room.create(client, max_peers: @max_peers)
assert {:error, _reason} = Room.create(client, max_peers: @max_peers)
end
end

Expand Down Expand Up @@ -432,8 +432,7 @@ defmodule Jellyfish.RoomTest do

error_msg = "Request failed: Reached peer limit in room #{room_id}"

assert {:error, ^error_msg} =
Room.add_peer(client, room_id, @peer_opts)
assert {:error, ^error_msg} = Room.add_peer(client, room_id, @peer_opts)
end
end

Expand All @@ -450,39 +449,59 @@ defmodule Jellyfish.RoomTest do
end
end

describe "Room.hls_subscribe/3" do
describe "Room.subscribe/4" do
setup [:create_room]

test "when request is valid", %{client: client, room_id: room_id} do
assert {:ok, %Component{properties: %{subscribe_mode: "manual"}}} =
test "when request is valid HLS", %{client: client, room_id: room_id} do
assert {:ok, %Component{id: id, properties: %{subscribe_mode: "manual"}}} =
Room.add_component(client, room_id, %Component.HLS{subscribe_mode: :manual})

assert :ok = Room.hls_subscribe(client, room_id, @origins)
assert :ok = Room.subscribe(client, room_id, id, @origins)
end

test "when request is valid recording", %{client: client, room_id: room_id} do
assert {:ok, %Component{id: id, properties: %{subscribe_mode: "manual"}}} =
Room.add_component(client, room_id, %{
@recording_component_opts
| subscribe_mode: :manual
})

assert :ok = Room.subscribe(client, room_id, id, @origins)
end

test "when room doesn't exist", %{client: client} do
assert {:error, "Request failed: Room #{@invalid_room_id} does not exist"} =
Room.hls_subscribe(client, @invalid_room_id, @origins)
Room.subscribe(client, @invalid_room_id, @invalid_component_id, @origins)
end

test "when hls component doesn't exist", %{client: client, room_id: room_id} do
assert {:error, "Request failed: HLS component does not exist"} =
Room.hls_subscribe(client, room_id, @origins)
test "when component doesn't exist", %{client: client, room_id: room_id} do
assert {:error, "Request failed: Component #{@invalid_component_id} does not exist"} =
Room.subscribe(client, room_id, @invalid_component_id, @origins)
end

test "when hls component has subscribe mode :auto", %{client: client, room_id: room_id} do
assert {:ok, %Component{properties: %{subscribe_mode: "auto"}}} =
test "when component has subscribe mode :auto", %{client: client, room_id: room_id} do
assert {:ok, %Component{id: id, properties: %{subscribe_mode: "auto"}}} =
Room.add_component(client, room_id, %Jellyfish.Component.HLS{subscribe_mode: :auto})

assert {:error, "Request failed: HLS component option `subscribe_mode` is set to :auto"} =
Room.hls_subscribe(client, room_id, @origins)
text = "Request failed: Component #{id} option `subscribe_mode` is set to :auto"

assert {:error, ^text} = Room.subscribe(client, room_id, id, @origins)
end

test "when request is invalid", %{client: client, room_id: room_id} do
assert {:ok, %Component{properties: %{subscribe_mode: "manual"}}} =
assert {:ok, %Component{id: id, properties: %{subscribe_mode: "manual"}}} =
Room.add_component(client, room_id, %Component.HLS{subscribe_mode: :manual})

assert {:error, :origins_validation} = Room.hls_subscribe(client, room_id, @invalid_origins)
assert {:error, :origins_validation} = Room.subscribe(client, room_id, id, @invalid_origins)
end

test "when request subscribe for invalid component", %{client: client, room_id: room_id} do
assert {:ok, %Component{id: id, properties: _properties}} =
Room.add_component(client, room_id, @rtsp_component_opts)

assert {:error,
"Request failed: Subscribe mode is supported only for HLS and Recording components"} =
Room.subscribe(client, room_id, id, @origins)
end
end

Expand Down Expand Up @@ -516,8 +535,7 @@ defmodule Jellyfish.RoomTest do

text = "Request failed: Component #{component_id} is not a SIP component"

assert {:error, ^text} =
Room.dial(client, room_id, component_id, @sip_phone_number)
assert {:error, ^text} = Room.dial(client, room_id, component_id, @sip_phone_number)
end

test "when request is invalid", %{client: client, room_id: room_id} do
Expand Down

0 comments on commit d71c89d

Please sign in to comment.