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

Fix stream format error message, fix race condition in handling child pad removed #591

Merged
merged 5 commits into from
Aug 23, 2023
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
13 changes: 3 additions & 10 deletions lib/membrane/core/element/action_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -362,19 +362,12 @@ defmodule Membrane.Core.Element.ActionHandler do
pid: pid,
other_ref: other_ref,
name: pad_name,
stream_format_validation_params: stream_format_validation_params
stream_format_validation_params: validation_params
} <- pad_data do
stream_format_validation_params = [
{state.module, pad_name} | stream_format_validation_params
]
validation_params = [{state.module, pad_name} | validation_params]

:ok =
StreamFormatController.validate_stream_format!(
:output,
stream_format_validation_params,
stream_format,
state
)
StreamFormatController.validate_stream_format!(:output, validation_params, stream_format)

state = PadModel.set_data!(state, pad_ref, :stream_format, stream_format)
Message.send(pid, :stream_format, stream_format, for_pad: other_ref)
Expand Down
22 changes: 9 additions & 13 deletions lib/membrane/core/element/stream_format_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,15 @@ defmodule Membrane.Core.Element.StreamFormatController do
State.t()
def exec_handle_stream_format(pad_ref, stream_format, params \\ %{}, state) do
%{
stream_format_validation_params: stream_format_validation_params,
stream_format_validation_params: validation_params,
name: pad_name,
stream_format: old_stream_format
} = PadModel.get_data!(state, pad_ref)

context = &CallbackContext.from_state(&1, old_stream_format: old_stream_format)
validation_params = [{state.module, pad_name} | validation_params]
:ok = validate_stream_format!(:input, validation_params, stream_format)

:ok =
validate_stream_format!(
:input,
[{state.module, pad_name} | stream_format_validation_params],
stream_format,
state
)
context = &CallbackContext.from_state(&1, old_stream_format: old_stream_format)

state =
CallbackHandler.exec_and_handle_callback(
Expand All @@ -82,10 +77,9 @@ defmodule Membrane.Core.Element.StreamFormatController do
@spec validate_stream_format!(
Pad.direction(),
stream_format_validation_params(),
StreamFormat.t(),
State.t()
StreamFormat.t()
) :: :ok
def validate_stream_format!(direction, params, stream_format, state) do
def validate_stream_format!(direction, params, stream_format) do
unless is_struct(stream_format) do
raise Membrane.StreamFormatError, """
Stream format must be defined as a struct, therefore it cannot be: #{inspect(stream_format)}
Expand All @@ -94,7 +88,9 @@ defmodule Membrane.Core.Element.StreamFormatController do

for {module, pad_name} <- params do
unless module.membrane_stream_format_match?(pad_name, stream_format) do
pattern_string = get_in(state, [:pads_info, pad_name, :accepted_formats_str])
pattern_string =
module.membrane_pads()
|> get_in([pad_name, :accepted_formats_str])

raise Membrane.StreamFormatError, """
Stream format: #{inspect(stream_format)} is not matching accepted format pattern "#{pattern_string}" in def_#{direction}_pad
Expand Down
17 changes: 8 additions & 9 deletions lib/membrane/core/parent/child_life_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -611,24 +611,23 @@ defmodule Membrane.Core.Parent.ChildLifeController do
end

@spec handle_child_pad_removed(Child.name(), Pad.ref(), Parent.state()) :: Parent.state()
def handle_child_pad_removed(child, pad, state) do
Membrane.Logger.debug_verbose("Child #{inspect(child)} removed pad #{inspect(pad)}")
def handle_child_pad_removed(child, child_pad_ref, state) do
Membrane.Logger.debug_verbose("Child #{inspect(child)} removed pad #{inspect(child_pad_ref)}")

child_terminating? = Parent.ChildrenModel.get_child_data!(state, child).terminating?

if child_terminating? do
state
else
with %{terminating?: false} <- Map.get(state.children, child),
{:ok, _link} <- LinkUtils.get_link(child, child_pad_ref, state) do
state =
CallbackHandler.exec_and_handle_callback(
:handle_child_pad_removed,
Component.action_handler(state),
%{context: &Component.context_from_state/1},
[child, pad],
[child, child_pad_ref],
state
)

LinkUtils.handle_child_pad_removed(child, pad, state)
LinkUtils.handle_child_pad_removed(child, child_pad_ref, state)
else
_other -> state
end
end

Expand Down
12 changes: 7 additions & 5 deletions lib/membrane/core/parent/child_life_controller/link_utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ defmodule Membrane.Core.Parent.ChildLifeController.LinkUtils do

@spec handle_child_pad_removed(Child.name(), Pad.ref(), Parent.state()) :: Parent.state()
def handle_child_pad_removed(child, pad, state) do
{:ok, link} = get_link(state.links, child, pad)
{:ok, link} = get_link(child, pad, state)

state =
opposite_endpoint(link, child)
Expand All @@ -57,7 +57,7 @@ defmodule Membrane.Core.Parent.ChildLifeController.LinkUtils do

@spec remove_link(Child.name(), Pad.ref(), Parent.state()) :: Parent.state()
def remove_link(child_name, pad_ref, state) do
with {:ok, link} <- get_link(state.links, child_name, pad_ref) do
with {:ok, link} <- get_link(child_name, pad_ref, state) do
state =
[link.to, link.from]
|> Enum.reduce(state, &unlink_endpoint/2)
Expand Down Expand Up @@ -168,10 +168,12 @@ defmodule Membrane.Core.Parent.ChildLifeController.LinkUtils do
links
end

defp get_link(links, child, pad) do
Enum.find(links, fn {_id, link} ->
@spec get_link(Child.name(), Pad.ref(), Parent.state()) ::
{:ok, Link.t()} | {:error, :not_found}
def get_link(child, child_pad_ref, state) do
Enum.find(state.links, fn {_id, link} ->
[link.from, link.to]
|> Enum.any?(&(&1.child == child and &1.pad_ref == pad))
|> Enum.any?(&(&1.child == child and &1.pad_ref == child_pad_ref))
end)
|> case do
{_id, %Link{} = link} -> {:ok, link}
Expand Down