Skip to content
This repository was archived by the owner on Sep 19, 2024. It is now read-only.

Commit a4f5c8a

Browse files
committed
Refactor code
1 parent a43e56c commit a4f5c8a

File tree

3 files changed

+79
-85
lines changed

3 files changed

+79
-85
lines changed

lib/membrane_rtc_engine/endpoints/webrtc/simulcast_tee.ex

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ defmodule Membrane.RTC.Engine.Endpoint.WebRTC.SimulcastTee do
3030
spec: String.t() | nil,
3131
default: nil,
3232
description: """
33-
TODO
33+
Initial encoding that should be sent via this pad.
34+
`nil` means that the best possible encoding should be used.
3435
"""
3536
]
3637
]

lib/membrane_rtc_engine/engine.ex

+67-68
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,10 @@ defmodule Membrane.RTC.Engine do
165165
Where `caps` are `t:Membrane.Caps.t/0` or `:any`.
166166
167167
* publish for some tracks using actions `t:publish_action_t/0` and subscribe for some tracks using
168-
function `#{inspect(__MODULE__)}.subscribe/3`. The first will cause RTC Engine to send a message in
168+
function `#{inspect(__MODULE__)}.subscribe/5`. The first will cause RTC Engine to send a message in
169169
form of `{:new_tracks, tracks}` where `tracks` is a list of `t:#{inspect(__MODULE__)}.Track.t/0` to all other Endpoints.
170170
When an Endpoint receives such a message it can subscribe for new tracks by
171-
using `#{inspect(__MODULE__)}.subscribe/3` function. An Endpoint will be notified about track readiness
171+
using `#{inspect(__MODULE__)}.subscribe/5` function. An Endpoint will be notified about track readiness
172172
it subscribed for in `c:Membrane.Bin.handle_pad_added/3` callback. An example implementation of `handle_pad_added`
173173
callback can look like this
174174
@@ -240,6 +240,14 @@ defmodule Membrane.RTC.Engine do
240240
node: node()
241241
]
242242

243+
@typedoc """
244+
Subscription options.
245+
246+
* `default_simulcast_encoding` - initial encoding that
247+
endpoint making subscription wants to receive
248+
"""
249+
@type subscription_opts_t() :: [default_simulcast_encoding: String.t()]
250+
243251
@typedoc """
244252
Membrane action that will cause RTC Engine to publish some message to all other endpoints.
245253
"""
@@ -406,8 +414,6 @@ defmodule Membrane.RTC.Engine do
406414
:ok
407415
end
408416

409-
@type subscription_opts_t() :: [default_simulcast_encoding: String.t()]
410-
411417
@doc """
412418
Subscribes endpoint for tracks.
413419
@@ -588,14 +594,12 @@ defmodule Membrane.RTC.Engine do
588594
endpoint_id: endpoint_id,
589595
track_id: track_id,
590596
format: format,
591-
opts: opts,
592-
status: :created
597+
opts: opts
593598
}
594599

595600
case check_subscription(subscription, state) do
596601
:ok ->
597602
{links, state} = try_fulfill_subscription(subscription, ctx, state)
598-
599603
parent_spec = %ParentSpec{links: links, log_metadata: [rtc: state.id]}
600604
send(endpoint_pid, {ref, :ok})
601605
{{:ok, [spec: parent_spec]}, state}
@@ -639,34 +643,6 @@ defmodule Membrane.RTC.Engine do
639643
{:ok, state}
640644
end
641645

642-
defp check_subscription(subscription, state) do
643-
# checks whether subscription is proper
644-
track =
645-
state.endpoints
646-
|> Map.values()
647-
|> Enum.flat_map(&Endpoint.get_tracks/1)
648-
|> Map.new(&{&1.id, &1})
649-
|> Map.get(subscription.track_id)
650-
651-
default_simulcast_encoding = subscription.opts[:default_simulcast_encoding]
652-
653-
cond do
654-
track == nil ->
655-
{:error, :invalid_track_id}
656-
657-
subscription.format not in track.format ->
658-
{:error, :invalid_format}
659-
660-
# TODO maybe simulcast_encodings should be always a list
661-
default_simulcast_encoding != nil and track.simulcast_encodings != nil and
662-
default_simulcast_encoding not in track.simulcast_encodings ->
663-
{:error, :invalid_default_simulcast_encoding}
664-
665-
true ->
666-
:ok
667-
end
668-
end
669-
670646
defp handle_media_event(%{type: :join, data: data}, peer_id, _ctx, state) do
671647
peer = Peer.new(peer_id, data.metadata || %{})
672648
dispatch(%Message.NewPeer{rtc_engine: self(), peer: peer})
@@ -995,45 +971,67 @@ defmodule Membrane.RTC.Engine do
995971
{endpoint_to_tee_links ++ links, state}
996972
end
997973

974+
defp check_subscription(subscription, state) do
975+
# checks whether subscription is correct
976+
track =
977+
state.endpoints
978+
|> Map.values()
979+
|> Enum.flat_map(&Endpoint.get_tracks/1)
980+
|> Map.new(&{&1.id, &1})
981+
|> Map.get(subscription.track_id)
982+
983+
default_simulcast_encoding = subscription.opts[:default_simulcast_encoding]
984+
985+
cond do
986+
track == nil ->
987+
{:error, :invalid_track_id}
988+
989+
subscription.format not in track.format ->
990+
{:error, :invalid_format}
991+
992+
# TODO maybe simulcast_encodings should be always a list
993+
default_simulcast_encoding != nil and track.simulcast_encodings != nil and
994+
default_simulcast_encoding not in track.simulcast_encodings ->
995+
{:error, :invalid_default_simulcast_encoding}
996+
997+
true ->
998+
:ok
999+
end
1000+
end
1001+
9981002
defp try_fulfill_subscription(subscription, ctx, state) do
1003+
# if tee for this track is already spawned, fulfill subscription
1004+
# otherwise, save subscription as pending, we will fulfill it
1005+
# when tee appears
9991006
if Map.has_key?(ctx.children, {:tee, subscription.track_id}) do
10001007
fulfill_subscription(subscription, ctx, state)
10011008
else
1002-
subscription = %Subscription{subscription | status: :pending}
10031009
state = update_in(state, [:pending_subscriptions], &[subscription | &1])
10041010
{[], state}
10051011
end
10061012
end
10071013

10081014
defp fulfill_subscription(%Subscription{format: :raw} = s, ctx, state) do
1009-
links =
1015+
raw_format_links =
10101016
if Map.has_key?(ctx.children, {:raw_format_tee, s.track_id}) do
10111017
[]
10121018
else
10131019
prepare_raw_format_links(s.track_id, state)
1014-
end ++
1015-
prepare_track_to_endpoint_links(s, :raw_format_tee, state)
1020+
end
10161021

1017-
state =
1018-
update_in(
1019-
state,
1020-
[:subscriptions, s.endpoint_id],
1021-
&Map.put(&1, s.track_id, %Subscription{s | status: :active})
1022-
)
1022+
{links, state} = do_fulfill_subscription(s, :raw_format_tee, state)
10231023

1024-
{links, state}
1024+
{raw_format_links ++ links, state}
10251025
end
10261026

1027-
defp fulfill_subscription(s, _ctx, state) do
1028-
links = prepare_track_to_endpoint_links(s, :tee, state)
1029-
1030-
state =
1031-
update_in(
1032-
state,
1033-
[:subscriptions, s.endpoint_id],
1034-
&Map.put(&1, s.track_id, %Subscription{s | status: :active})
1035-
)
1027+
defp fulfill_subscription(%Subscription{format: _remote_format} = s, _ctx, state) do
1028+
do_fulfill_subscription(s, :tee, state)
1029+
end
10361030

1031+
defp do_fulfill_subscription(s, tee_kind, state) do
1032+
links = prepare_track_to_endpoint_links(s, tee_kind, state)
1033+
s = %Subscription{s | status: :active}
1034+
state = update_in(state, [:subscriptions, s.endpoint_id], &Map.put(&1, s.track_id, s))
10371035
{links, state}
10381036
end
10391037

@@ -1046,6 +1044,8 @@ defmodule Membrane.RTC.Engine do
10461044
end
10471045

10481046
defp prepare_track_to_endpoint_links(subscription, :tee, state) do
1047+
# if someone subscribed for simulcast track, prepare options
1048+
# for SimulcastTee
10491049
track =
10501050
state.endpoints
10511051
|> Map.values()
@@ -1175,28 +1175,27 @@ defmodule Membrane.RTC.Engine do
11751175
end
11761176
end
11771177

1178-
# TODO `peer_id` -> `endpoint_id`
1179-
defp do_remove_endpoint(peer_id, ctx, state) do
1180-
if Map.has_key?(state.endpoints, peer_id) do
1181-
{endpoint, state} = pop_in(state, [:endpoints, peer_id])
1178+
defp do_remove_endpoint(endpoint_id, ctx, state) do
1179+
if Map.has_key?(state.endpoints, endpoint_id) do
1180+
{endpoint, state} = pop_in(state, [:endpoints, endpoint_id])
1181+
{_subscriptions, state} = pop_in(state, [:subscriptions, endpoint_id])
11821182

11831183
state =
1184-
update_in(state, [:waiting_subscriptions, peer_id], fn subscriptions ->
1185-
Enum.filter(subscriptions, fn s -> s.endpoint_id != peer_id end)
1184+
update_in(state, [:pending_subscriptions, endpoint_id], fn subscriptions ->
1185+
Enum.filter(subscriptions, fn s -> s.endpoint_id != endpoint_id end)
11861186
end)
11871187

1188-
{_subscriptions, state} = pop_in(state, [:subscriptions, peer_id])
11891188
tracks = Enum.map(Endpoint.get_tracks(endpoint), &%Track{&1 | active?: true})
11901189

1191-
tracks_msgs = do_publish({:remove_tracks, tracks}, {:endpoint, peer_id}, state)
1190+
tracks_msgs = do_publish({:remove_tracks, tracks}, {:endpoint, endpoint_id}, state)
11921191

1193-
endpoint_bin = ctx.children[{:endpoint, peer_id}]
1192+
endpoint_bin = ctx.children[{:endpoint, endpoint_id}]
11941193

11951194
actions =
11961195
if endpoint_bin == nil or endpoint_bin.terminating? do
11971196
[]
11981197
else
1199-
[remove_child: find_children_for_endpoint(endpoint, peer_id, ctx)]
1198+
[remove_child: find_children_for_endpoint(endpoint, endpoint_id, ctx)]
12001199
end
12011200

12021201
{:present, tracks_msgs ++ actions, state}
@@ -1205,13 +1204,13 @@ defmodule Membrane.RTC.Engine do
12051204
end
12061205
end
12071206

1208-
defp find_children_for_endpoint(endpoint, peer_id, ctx) do
1207+
defp find_children_for_endpoint(endpoint, endpoint_id, ctx) do
12091208
children =
12101209
endpoint
12111210
|> Endpoint.get_tracks()
1212-
|> Enum.flat_map(fn track -> get_track_elements(peer_id, track.id, ctx) end)
1211+
|> Enum.flat_map(fn track -> get_track_elements(endpoint_id, track.id, ctx) end)
12131212

1214-
[endpoint: peer_id] ++ children
1213+
[endpoint: endpoint_id] ++ children
12151214
end
12161215

12171216
defp get_track_elements(endpoint_id, track_id, ctx) do
+10-16
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,26 @@
11
defmodule Membrane.RTC.Engine.Subscription do
22
@moduledoc false
33
# Module representing subscription for track
4+
alias Membrane.RTC.Engine
45
alias Membrane.RTC.Engine.{Endpoint, Track}
56

67
@typedoc """
7-
Subscription options.
8-
9-
* `default_simulcast_encoding` - initial encoding that
10-
endpoint making subscription wants to receive
11-
"""
12-
@type opts_t :: [default_simulcast_encoding: String.t()]
13-
14-
@typedoc """
15-
* `endpoint_id` - id of endpoint making subscription for track
8+
* `endpoint_id` - id of endpoint making subscription
169
* `track_id` - id of track endpoint subscribes for
1710
* `format` - format of track endpoint subscribes for
18-
* `status` - status of subscription. Subscription is `active` when
19-
given track is linked to given endpoint and `pending` otherwise
11+
* `status` - status of subscription. Subscription is
12+
`active` when track some endpoint subscribed for is linked
13+
to this endpoint and `pending` otherwise
2014
* `opts` - additional options
2115
"""
22-
@type t() :: %___MODULE__{
16+
@type t() :: %__MODULE__{
2317
endpoint_id: Endpoint.id(),
2418
track_id: Track.id(),
2519
format: Track.format(),
26-
status: :created | :pending | :active,
27-
opts: opts_t()
20+
status: :pending | :active,
21+
opts: Engine.subscription_opts_t()
2822
}
2923

30-
@enforce_keys [:endpoint_id, :track_id, :format, :status, :opts]
31-
defstruct @enforce_keys
24+
@enforce_keys [:endpoint_id, :track_id, :format]
25+
defstruct @enforce_keys ++ [status: :pending, opts: []]
3226
end

0 commit comments

Comments
 (0)