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
25 changes: 17 additions & 8 deletions lib/jellyfish/component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ defmodule Jellyfish.Component do

@enforce_keys [
:id,
:type
:type,
:metadata
]
defstruct @enforce_keys

Expand All @@ -36,7 +37,8 @@ defmodule Jellyfish.Component do
"""
@type t :: %__MODULE__{
id: id(),
type: type()
type: type(),
metadata: map()
}

@doc false
Expand All @@ -45,11 +47,15 @@ defmodule Jellyfish.Component do
case response do
%{
"id" => id,
"type" => type_str
"type" => type_str,
"metadata" => metadata
} ->
type = type_from_string(type_str)

%__MODULE__{
id: id,
type: type_from_string(type_str)
type: type,
metadata: type.metadata_from_json(metadata)
}

_other ->
Expand All @@ -63,11 +69,14 @@ defmodule Jellyfish.Component do
case response do
%RoomState.Component{
id: id,
type: type
component: {_type, component}
} ->
type = type_from_proto(component)

%__MODULE__{
id: id,
type: type_from_proto(type)
type: type,
metadata: type.metadata_from_proto(component)
}

_other ->
Expand All @@ -83,6 +92,6 @@ defmodule Jellyfish.Component do
defp type_from_string("hls"), do: HLS
defp type_from_string("rtsp"), do: RTSP

defp type_from_proto(:TYPE_HLS), do: HLS
defp type_from_proto(:TYPE_RTSP), do: RTSP
defp type_from_proto(%RoomState.Component.Hls{}), do: HLS
defp type_from_proto(%RoomState.Component.Rtsp{}), do: RTSP
end
5 changes: 5 additions & 0 deletions lib/jellyfish/component/deserializer.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
defmodule Jellyfish.Component.Deserializer do
@moduledoc false
@callback metadata_from_json(map()) :: map()
@callback metadata_from_proto(map()) :: map()
end
11 changes: 11 additions & 0 deletions lib/jellyfish/component/hls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,19 @@ defmodule Jellyfish.Component.HLS do
documentation](https://jellyfish-dev.github.io/jellyfish-docs/getting_started/components/hls).
"""

@behaviour Jellyfish.Component.Deserializer
@enforce_keys []
defstruct @enforce_keys ++ []

@type t :: %__MODULE__{}

@impl true
def metadata_from_json(%{"playable" => playable}) do
%{playable: playable}
end
Comment thread
LVala marked this conversation as resolved.

@impl true
def metadata_from_proto(%{playable: playable}) do
%{playable: playable}
end
end
6 changes: 6 additions & 0 deletions lib/jellyfish/component/rtsp.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ defmodule Jellyfish.Component.RTSP do
documentation](https://jellyfish-dev.github.io/jellyfish-docs/getting_started/components/rtsp).
"""

@behaviour Jellyfish.Component.Deserializer

@enforce_keys [:source_uri]
defstruct @enforce_keys ++
[
Expand All @@ -22,4 +24,8 @@ defmodule Jellyfish.Component.RTSP do
keep_alive_interval: non_neg_integer(),
pierce_nat: boolean()
}
@impl true
def metadata_from_json(_metadata), do: %{}
@impl true
def metadata_from_proto(_metadata), do: %{}
end
12 changes: 12 additions & 0 deletions lib/jellyfish/notification.ex
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ defmodule Jellyfish.Notification do
}
end

defmodule HlsPlayable do
@moduledoc nil

@enforce_keys [:room_id, :component_id]
defstruct @enforce_keys

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

@discarded_fields [:__unknown_fields__]

@doc false
Expand Down
47 changes: 34 additions & 13 deletions lib/protos/jellyfish/server_notifications.pb.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,6 @@ defmodule Jellyfish.ServerMessage.SubscribeResponse.RoomState.Peer.Status do
field :STATUS_DISCONNECTED, 2
end

defmodule Jellyfish.ServerMessage.SubscribeResponse.RoomState.Component.Type do
@moduledoc false

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

field :TYPE_UNSPECIFIED, 0
field :TYPE_HLS, 1
field :TYPE_RTSP, 2
end

defmodule Jellyfish.ServerMessage.RoomCrashed do
@moduledoc false

Expand Down Expand Up @@ -168,16 +158,33 @@ defmodule Jellyfish.ServerMessage.SubscribeResponse.RoomState.Peer do
enum: true
end

defmodule Jellyfish.ServerMessage.SubscribeResponse.RoomState.Component.Hls do
@moduledoc false

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

field :playable, 1, type: :bool
end

defmodule Jellyfish.ServerMessage.SubscribeResponse.RoomState.Component.Rtsp do
@moduledoc false

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

defmodule Jellyfish.ServerMessage.SubscribeResponse.RoomState.Component do
@moduledoc false

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

oneof :component, 0

field :id, 1, type: :string
field :hls, 2, type: Jellyfish.ServerMessage.SubscribeResponse.RoomState.Component.Hls, oneof: 0

field :type, 2,
type: Jellyfish.ServerMessage.SubscribeResponse.RoomState.Component.Type,
enum: true
field :rtsp, 3,
type: Jellyfish.ServerMessage.SubscribeResponse.RoomState.Component.Rtsp,
oneof: 0
end

defmodule Jellyfish.ServerMessage.SubscribeResponse.RoomState do
Expand Down Expand Up @@ -259,6 +266,15 @@ defmodule Jellyfish.ServerMessage.MetricsReport do
field :metrics, 1, type: :string
end

defmodule Jellyfish.ServerMessage.HlsPlayable do
@moduledoc false

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

field :room_id, 1, type: :string, json_name: "roomId"
field :component_id, 2, type: :string, json_name: "componentId"
end

defmodule Jellyfish.ServerMessage do
@moduledoc false

Expand Down Expand Up @@ -322,4 +338,9 @@ defmodule Jellyfish.ServerMessage do
type: Jellyfish.ServerMessage.MetricsReport,
json_name: "metricsReport",
oneof: 0

field :hls_playable, 13,
type: Jellyfish.ServerMessage.HlsPlayable,
json_name: "hlsPlayable",
oneof: 0
end
2 changes: 1 addition & 1 deletion protos
12 changes: 12 additions & 0 deletions test/jellyfish/notifier_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule Jellyfish.NotifierTest do
use ExUnit.Case

alias Jellyfish.Component.HLS
alias Jellyfish.{Client, Notifier, Peer, Room}

alias Jellyfish.PeerMessage
Expand Down Expand Up @@ -58,6 +59,17 @@ defmodule Jellyfish.NotifierTest do
Notifier.subscribe_server_notifications(notifier, room_id)
end

test "returns state of the room with hls", %{client: client, notifier: notifier} do
{:ok, %Jellyfish.Room{id: room_id}} = Room.create(client, video_codec: :h264)
{:ok, %Jellyfish.Component{id: component_id}} = Room.add_component(client, room_id, HLS)

assert {:ok, %Room{id: ^room_id, components: [component]}} =
Notifier.subscribe_server_notifications(notifier, room_id)

assert %Jellyfish.Component{id: ^component_id, type: HLS, metadata: %{playable: false}} =
component
end

test "for all notifications", %{client: client, notifier: notifier} do
{:ok, %Jellyfish.Room{id: room_id}} = Room.create(client)

Expand Down
35 changes: 23 additions & 12 deletions test/jellyfish/room_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ defmodule Jellyfish.RoomTest do

@server_api_token "development"

@component_opts %Component.HLS{}
@component_opts_module Component.HLS
@hls_component_opts %Component.HLS{}
@hls_component_opts_module Component.HLS

@rtsp_component_opts %Component.RTSP{
source_uri: "rtsp://ef36c6dff23ecc5bbe311cc880d95dc8.se:2137/does/not/matter"
}

@peer_opts %Peer.WebRTC{}
@peer_opts_module Peer.WebRTC

Expand Down Expand Up @@ -97,16 +102,19 @@ defmodule Jellyfish.RoomTest do
end

describe "Room.get/2" do
setup [:create_room]
setup [:create_room, :create_component_hls]

test "when request is valid", %{client: client, room_id: room_id} do
test "when request is valid", %{client: client, room_id: room_id, component_id: component_id} do
assert {:ok,
%Jellyfish.Room{
components: [],
components: [component],
config: %{max_peers: @max_peers, video_codec: @video_codec},
id: ^room_id,
peers: []
}} = Room.get(client, room_id)

assert %Component{id: ^component_id, type: Component.HLS, metadata: %{playable: false}} =
component
end

test "when request is invalid", %{client: client} do
Expand All @@ -119,11 +127,14 @@ defmodule Jellyfish.RoomTest do
setup [:create_room]

test "when request is valid", %{client: client, room_id: room_id} do
assert {:ok, component} = Room.add_component(client, room_id, @component_opts)
assert %Jellyfish.Component{type: Component.HLS} = component
assert {:ok, component} = Room.add_component(client, room_id, @hls_component_opts)
assert %Component{type: Component.HLS, metadata: %{playable: false}} = component

assert {:ok, component} = Room.add_component(client, room_id, @hls_component_opts_module)
assert %Component{type: Component.HLS, metadata: %{playable: false}} = component

assert {:ok, component} = Room.add_component(client, room_id, @component_opts_module)
assert %Jellyfish.Component{type: Component.HLS} = component
assert {:ok, component} = Room.add_component(client, room_id, @rtsp_component_opts)
assert %Component{type: Component.RTSP, metadata: %{}} = component
end

test "when request is invalid", %{client: client} do
Expand All @@ -138,7 +149,7 @@ defmodule Jellyfish.RoomTest do
end

describe "Room.delete_component/3" do
setup [:create_room, :create_component]
setup [:create_room, :create_component_hls]

test "when request is valid", %{client: client, room_id: room_id, component_id: component_id} do
assert :ok = Room.delete_component(client, room_id, component_id)
Expand Down Expand Up @@ -199,9 +210,9 @@ defmodule Jellyfish.RoomTest do
%{peer_id: id}
end

defp create_component(state) do
defp create_component_hls(state) do
assert {:ok, %Jellyfish.Component{id: id}} =
Room.add_component(state.client, state.room_id, @component_opts)
Room.add_component(state.client, state.room_id, @hls_component_opts)

%{component_id: id}
end
Expand Down