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
22 changes: 22 additions & 0 deletions lib/jellyfish/notification.ex
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,28 @@ defmodule Jellyfish.Notification do
}
end

defmodule HlsUploaded do
Comment thread
Karolk99 marked this conversation as resolved.
@moduledoc nil

@enforce_keys [:room_id]
defstruct @enforce_keys

@type t :: %__MODULE__{
room_id: Room.id()
}
end

defmodule HlsUploadCrashed do
@moduledoc nil

@enforce_keys [:room_id]
defstruct @enforce_keys

@type t :: %__MODULE__{
room_id: Room.id()
}
end

@discarded_fields [:__unknown_fields__]

@doc false
Expand Down
20 changes: 13 additions & 7 deletions lib/jellyfish/room.ex
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,19 @@ defmodule Jellyfish.Room do
end

@doc """
Adds tracks to hls component
Adds peers and components tracks to hls component
Comment thread
mickel8 marked this conversation as resolved.

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.
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(), [track_id()]) :: :ok | {:error, atom() | String.t()}
def hls_subscribe(client, room_id, tracks) do
with :ok <- validate_tracks(tracks),
@spec hls_subscribe(Client.t(), id(), [Peer.id() | Component.id()]) ::
:ok | {:error, atom() | String.t()}
def hls_subscribe(client, room_id, origins) do
with :ok <- validate_origins(origins),
{:ok, %Env{status: 201}} <-
Tesla.post(client.http_client, "/hls/#{room_id}/subscribe", %{tracks: tracks}) do
Tesla.post(client.http_client, "/hls/#{room_id}/subscribe", %{origins: origins}) do
:ok
else
error -> Utils.handle_response_error(error)
Expand Down Expand Up @@ -306,8 +312,8 @@ defmodule Jellyfish.Room do
defp validate_subscribe_mode(mode) when mode in @subscribe_modes, do: :ok
defp validate_subscribe_mode(_mode), do: :error

defp validate_tracks(tracks) when is_list(tracks), do: :ok
defp validate_tracks(_tracks), do: {:error, :tracks_validation}
defp validate_origins(origins) when is_list(origins), do: :ok
defp validate_origins(_tracks), do: {:error, :origins_validation}

defp map_snake_case_to_camel_case(%{} = map),
do:
Expand Down
26 changes: 26 additions & 0 deletions lib/protos/jellyfish/server_notifications.pb.ex
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,22 @@ defmodule Jellyfish.ServerMessage.HlsPlayable do
field :component_id, 2, type: :string, json_name: "componentId"
end

defmodule Jellyfish.ServerMessage.HlsUploaded do
@moduledoc false

use Protobuf, syntax: :proto3, protoc_gen_elixir_version: "0.12.0"

field :room_id, 1, type: :string, json_name: "roomId"
end

defmodule Jellyfish.ServerMessage.HlsUploadCrashed do
@moduledoc false

use Protobuf, syntax: :proto3, protoc_gen_elixir_version: "0.12.0"

field :room_id, 1, type: :string, json_name: "roomId"
end

defmodule Jellyfish.ServerMessage do
@moduledoc false

Expand Down Expand Up @@ -189,4 +205,14 @@ defmodule Jellyfish.ServerMessage do
type: Jellyfish.ServerMessage.HlsPlayable,
json_name: "hlsPlayable",
oneof: 0

field :hls_uploaded, 14,
type: Jellyfish.ServerMessage.HlsUploaded,
json_name: "hlsUploaded",
oneof: 0

field :hls_upload_crashed, 15,
type: Jellyfish.ServerMessage.HlsUploadCrashed,
json_name: "hlsUploadCrashed",
oneof: 0
end
2 changes: 1 addition & 1 deletion protos
14 changes: 7 additions & 7 deletions test/jellyfish/room_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ defmodule Jellyfish.RoomTest do
@invalid_max_peers "abc"
@invalid_video_codec :opus

@tracks ["track-id"]
@invalid_tracks %{id: "track-id"}
@origins ["peer-id", "rtsp-id", "file-id"]
@invalid_origins %{id: "peer-id"}

@invalid_peer_id "invalid_peer_id"
defmodule InvalidPeerOpts do
Expand Down Expand Up @@ -308,32 +308,32 @@ defmodule Jellyfish.RoomTest do
assert {:ok, %Component{properties: %{subscribe_mode: "manual"}}} =
Room.add_component(client, room_id, %Component.HLS{subscribe_mode: :manual})

assert :ok = Room.hls_subscribe(client, room_id, @tracks)
assert :ok = Room.hls_subscribe(client, room_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, @tracks)
Room.hls_subscribe(client, @invalid_room_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, @tracks)
Room.hls_subscribe(client, room_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"}}} =
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, @tracks)
Room.hls_subscribe(client, room_id, @origins)
end

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

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

Expand Down