Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate warnings related to pads with :auto flow control #900

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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: 25 additions & 0 deletions lib/membrane/core/child/pads_specs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ defmodule Membrane.Core.Child.PadsSpecs do
defmacro generate_membrane_pads(env) do
pads = Module.get_attribute(env.module, :membrane_pads, []) |> Enum.reverse()
:ok = validate_pads!(pads, env)
:ok = generate_flow_control_related_warnings(pads, env)

alias Membrane.Pad

Expand All @@ -153,6 +154,30 @@ defmodule Membrane.Core.Child.PadsSpecs do
end
end

@spec generate_flow_control_related_warnings(
pads :: [{Pad.name(), Pad.description()}],
env :: Macro.Env.t()
) :: :ok
defp generate_flow_control_related_warnings(pads, env) do
{auto_pads, push_pads} =
pads
|> Enum.filter(fn {_pad, info} -> info[:flow_control] in [:auto, :push] end)
|> Enum.split_with(fn {_pad, info} -> info.flow_control == :auto end)

if auto_pads != [] and push_pads != [] do
IO.warn("""
#{inspect(env.module)} defines pads with `flow_control: :auto` and pads with `flow_control: :push` \
at the same time. Please, consider if this what you want to do - flow control of these pads won't be \
integrated. Setting `flow_control` to `:auto` in the places where it has been set to `:push` might be \
a good idea.
Pads with `flow_control: :auto`: #{auto_pads |> Keyword.keys() |> Enum.join(", ")}.
Pads with `flow_control: :push`: #{push_pads |> Keyword.keys() |> Enum.join(", ")}.
""")
end

:ok
end

@spec parse_pad_specs!(
specs :: Pad.spec(),
direction :: Pad.direction(),
Expand Down
30 changes: 30 additions & 0 deletions lib/membrane/core/element/pad_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,40 @@ defmodule Membrane.Core.Element.PadController do
end

state = maybe_handle_pad_added(input_endpoint.pad_ref, state)
:ok = generate_flow_control_related_warning(pad_info, state)

{{:ok, {input_endpoint, pad_info, link_metadata}}, state}
end

defp generate_flow_control_related_warning(%{flow_control: :auto} = _pad_info, state) do
{auto_input_pads, auto_output_pads} =
state.pads_data
|> Enum.filter(fn {_pad_ref, data} -> data.flow_control == :auto end)
|> Enum.split_with(fn {_pad_ref, data} -> data.direction == :input end)

if length(auto_input_pads) >= 2 and length(auto_output_pads) >= 2 do
[auto_input_pads, auto_output_pads] =
[auto_input_pads, auto_output_pads]
|> Enum.map(fn pads ->
pads
|> Enum.map(&(&1 |> elem(0) |> inspect()))
|> Enum.join(", ")
end)

Membrane.Logger.warning("""
This element has multiple input pads with `flow_control: :auto` and multiple output pads with \
`flow_control: :auto` at the same time. Notice, that lack of demand on any of output pads with \
`flow_control: :auto` will cause stoping demand on every input pad with `flow_control: :auto`.
Input pads with `flow_control: :auto`: #{auto_input_pads}.
Output pads with `flow_control: :auto`: #{auto_output_pads}.
""")
end

:ok
end

defp generate_flow_control_related_warning(_pad_info, _state), do: :ok

@doc """
Handles situation where pad has been unlinked (e.g. when connected element has been removed from pipeline)

Expand Down