diff --git a/lib/membrane/bin.ex b/lib/membrane/bin.ex index ca0fa0da1..b0085f9dc 100644 --- a/lib/membrane/bin.ex +++ b/lib/membrane/bin.ex @@ -44,7 +44,7 @@ defmodule Membrane.Bin do while `handle_init` should be used for things like parsing options, initializing state or spawning children. """ - @callback handle_init(context :: CallbackContext.Init.t(), options :: options_t) :: + @callback handle_init(context :: CallbackContext.t(), options :: options_t) :: callback_return_t() @doc """ @@ -53,7 +53,7 @@ defmodule Membrane.Bin do """ @callback handle_pad_added( pad :: Pad.ref_t(), - context :: CallbackContext.PadAdded.t(), + context :: CallbackContext.t(), state :: state_t ) :: callback_return_t @@ -63,7 +63,7 @@ defmodule Membrane.Bin do """ @callback handle_pad_removed( pad :: Pad.ref_t(), - context :: CallbackContext.PadRemoved.t(), + context :: CallbackContext.t(), state :: state_t ) :: callback_return_t @@ -73,7 +73,7 @@ defmodule Membrane.Bin do Any long-lasting or complex initialization should happen here. """ @callback handle_setup( - context :: CallbackContext.Setup.t(), + context :: CallbackContext.t(), state :: state_t ) :: callback_return_t @@ -81,7 +81,7 @@ defmodule Membrane.Bin do Callback invoked when bin switches the playback to `:playing`. """ @callback handle_playing( - context :: CallbackContext.Playing.t(), + context :: CallbackContext.t(), state :: state_t ) :: callback_return_t @@ -92,7 +92,7 @@ defmodule Membrane.Bin do @callback handle_child_notification( notification :: Membrane.ChildNotification.t(), element :: Child.name_t(), - context :: CallbackContext.ChildNotification.t(), + context :: CallbackContext.t(), state :: state_t ) :: callback_return_t @@ -101,7 +101,7 @@ defmodule Membrane.Bin do """ @callback handle_parent_notification( notification :: Membrane.ParentNotification.t(), - context :: CallbackContext.ParentNotification.t(), + context :: CallbackContext.t(), state :: state_t ) :: callback_return_t @@ -113,7 +113,7 @@ defmodule Membrane.Bin do """ @callback handle_info( message :: any, - context :: CallbackContext.Info.t(), + context :: CallbackContext.t(), state :: state_t ) :: callback_return_t @@ -123,7 +123,7 @@ defmodule Membrane.Bin do @callback handle_element_start_of_stream( child :: Child.name_t(), pad :: Pad.ref_t(), - context :: CallbackContext.StreamManagement.t(), + context :: CallbackContext.t(), state :: state_t ) :: callback_return_t @@ -133,7 +133,7 @@ defmodule Membrane.Bin do @callback handle_element_end_of_stream( child :: Child.name_t(), pad :: Pad.ref_t(), - context :: CallbackContext.StreamManagement.t(), + context :: CallbackContext.t(), state :: state_t ) :: callback_return_t @@ -142,7 +142,7 @@ defmodule Membrane.Bin do """ @callback handle_spec_started( children :: [Child.name_t()], - context :: CallbackContext.SpecStarted.t(), + context :: CallbackContext.t(), state :: state_t ) :: callback_return_t @@ -152,7 +152,7 @@ defmodule Membrane.Bin do """ @callback handle_tick( timer_id :: any, - context :: CallbackContext.Tick.t(), + context :: CallbackContext.t(), state :: state_t ) :: callback_return_t @@ -161,7 +161,10 @@ defmodule Membrane.Bin do By default it returns `t:Membrane.Bin.Action.terminate_t/0` with reason `:normal`. """ - @callback handle_terminate_request(context :: CallbackContext.TerminateRequest.t(), state_t) :: + @callback handle_terminate_request( + context :: CallbackContext.t(), + state_t + ) :: callback_return_t() @optional_callbacks handle_init: 2, diff --git a/lib/membrane/bin/callback_context.ex b/lib/membrane/bin/callback_context.ex new file mode 100644 index 000000000..1acfa7c24 --- /dev/null +++ b/lib/membrane/bin/callback_context.ex @@ -0,0 +1,36 @@ +defmodule Membrane.Bin.CallbackContext do + @moduledoc """ + Module describiing context passed to the `Membrane.Bin` callbacks. + """ + + @type t :: %{ + :clock => Membrane.Clock.t(), + :parent_clock => Membrane.Clock.t(), + :pads => %{Membrane.Pad.ref_t() => Membrane.Bin.PadData.t()}, + :name => Membrane.Bin.name_t(), + :children => %{Membrane.Child.name_t() => Membrane.ChildEntry.t()}, + :playback => Membrane.Playback.t(), + :resource_guard => Membrane.ResourceGuard.t(), + :utility_supervisor => Membrane.UtilitySupervisor.t(), + optional(:pad_options) => map() + } + + @type option_t() :: {:pad_options, map()} + + @type options_t :: [option_t()] + + @spec from_state(Membrane.Core.Bin.State.t(), options_t()) :: t() + def from_state(state, additional_fields \\ []) do + Map.new(additional_fields) + |> Map.merge(%{ + clock: state.synchronization.clock, + parent_clock: state.synchronization.parent_clock, + pads: state.pads_data, + name: state.name, + children: state.children, + playback: state.playback, + resource_guard: state.resource_guard, + utility_supervisor: state.subprocess_supervisor + }) + end +end diff --git a/lib/membrane/bin/callback_context/child_notification.ex b/lib/membrane/bin/callback_context/child_notification.ex deleted file mode 100644 index c807e2794..000000000 --- a/lib/membrane/bin/callback_context/child_notification.ex +++ /dev/null @@ -1,7 +0,0 @@ -defmodule Membrane.Bin.CallbackContext.ChildNotification do - @moduledoc """ - Structure representing a context that is passed to the callback when - bin receives child notification. - """ - use Membrane.Core.Bin.CallbackContext -end diff --git a/lib/membrane/bin/callback_context/crash_group_down.ex b/lib/membrane/bin/callback_context/crash_group_down.ex deleted file mode 100644 index 369c8ff98..000000000 --- a/lib/membrane/bin/callback_context/crash_group_down.ex +++ /dev/null @@ -1,9 +0,0 @@ -defmodule Membrane.Bin.CallbackContext.CrashGroupDown do - @moduledoc """ - Structure representing a context that is passed to the pipeline - when a crash group is down. - """ - use Membrane.Core.Bin.CallbackContext, - members: [Membrane.Child.name_t()], - crash_initiator: Membrane.Child.name_t() -end diff --git a/lib/membrane/bin/callback_context/info.ex b/lib/membrane/bin/callback_context/info.ex deleted file mode 100644 index 91086a54e..000000000 --- a/lib/membrane/bin/callback_context/info.ex +++ /dev/null @@ -1,7 +0,0 @@ -defmodule Membrane.Bin.CallbackContext.Info do - @moduledoc """ - Structure representing a context that is passed to the callback when - bin receives unrecognized message. - """ - use Membrane.Core.Bin.CallbackContext -end diff --git a/lib/membrane/bin/callback_context/init.ex b/lib/membrane/bin/callback_context/init.ex deleted file mode 100644 index 54e34715f..000000000 --- a/lib/membrane/bin/callback_context/init.ex +++ /dev/null @@ -1,6 +0,0 @@ -defmodule Membrane.Bin.CallbackContext.Init do - @moduledoc """ - Callback context for `c:Membrane.Bin.handle_init/2`. - """ - use Membrane.Core.Bin.CallbackContext -end diff --git a/lib/membrane/bin/callback_context/pad_added.ex b/lib/membrane/bin/callback_context/pad_added.ex deleted file mode 100644 index 0911fa851..000000000 --- a/lib/membrane/bin/callback_context/pad_added.ex +++ /dev/null @@ -1,9 +0,0 @@ -defmodule Membrane.Bin.CallbackContext.PadAdded do - @moduledoc """ - Structure representing a context that is passed to the bin - when a new dynamic pad instance added is created - """ - use Membrane.Core.Bin.CallbackContext, - direction: :input | :output, - options: map() -end diff --git a/lib/membrane/bin/callback_context/pad_removed.ex b/lib/membrane/bin/callback_context/pad_removed.ex deleted file mode 100644 index ece69a8e3..000000000 --- a/lib/membrane/bin/callback_context/pad_removed.ex +++ /dev/null @@ -1,8 +0,0 @@ -defmodule Membrane.Bin.CallbackContext.PadRemoved do - @moduledoc """ - Structure representing a context that is passed to the bin - when a dynamic pad is removed - """ - use Membrane.Core.Bin.CallbackContext, - direction: :input | :output -end diff --git a/lib/membrane/bin/callback_context/parent_notification.ex b/lib/membrane/bin/callback_context/parent_notification.ex deleted file mode 100644 index 86b30e9b5..000000000 --- a/lib/membrane/bin/callback_context/parent_notification.ex +++ /dev/null @@ -1,7 +0,0 @@ -defmodule Membrane.Bin.CallbackContext.ParentNotification do - @moduledoc """ - Structure representing a context that is passed to the callback when - bin receives parent notification. - """ - use Membrane.Core.Bin.CallbackContext -end diff --git a/lib/membrane/bin/callback_context/playing.ex b/lib/membrane/bin/callback_context/playing.ex deleted file mode 100644 index b4147a3fb..000000000 --- a/lib/membrane/bin/callback_context/playing.ex +++ /dev/null @@ -1,6 +0,0 @@ -defmodule Membrane.Bin.CallbackContext.Playing do - @moduledoc """ - Structure representing a context that is passed to the `c:Membrane.Bin.handle_playing/2` callback. - """ - use Membrane.Core.Bin.CallbackContext -end diff --git a/lib/membrane/bin/callback_context/setup.ex b/lib/membrane/bin/callback_context/setup.ex deleted file mode 100644 index 43da862df..000000000 --- a/lib/membrane/bin/callback_context/setup.ex +++ /dev/null @@ -1,6 +0,0 @@ -defmodule Membrane.Bin.CallbackContext.Setup do - @moduledoc """ - Structure representing a context that is passed to the `c:Membrane.Bin.handle_setup/2` callback. - """ - use Membrane.Core.Bin.CallbackContext -end diff --git a/lib/membrane/bin/callback_context/spec_started.ex b/lib/membrane/bin/callback_context/spec_started.ex deleted file mode 100644 index 1d41a4fc6..000000000 --- a/lib/membrane/bin/callback_context/spec_started.ex +++ /dev/null @@ -1,7 +0,0 @@ -defmodule Membrane.Bin.CallbackContext.SpecStarted do - @moduledoc """ - Structure representing a context that is passed to the callback of the bin - when it instantiates children and links them according to `Membrane.ChildrenSpec` - """ - use Membrane.Core.Bin.CallbackContext -end diff --git a/lib/membrane/bin/callback_context/stream_management.ex b/lib/membrane/bin/callback_context/stream_management.ex deleted file mode 100644 index 834f69e56..000000000 --- a/lib/membrane/bin/callback_context/stream_management.ex +++ /dev/null @@ -1,7 +0,0 @@ -defmodule Membrane.Bin.CallbackContext.StreamManagement do - @moduledoc """ - Structure representing a context that is passed to the bin - when handling start and end of stream events. - """ - use Membrane.Core.Bin.CallbackContext -end diff --git a/lib/membrane/bin/callback_context/terminate_request.ex b/lib/membrane/bin/callback_context/terminate_request.ex deleted file mode 100644 index e7bbdcf0d..000000000 --- a/lib/membrane/bin/callback_context/terminate_request.ex +++ /dev/null @@ -1,6 +0,0 @@ -defmodule Membrane.Bin.CallbackContext.TerminateRequest do - @moduledoc """ - Callback context for `c:Membrane.Bin.handle_terminate_request/2`. - """ - use Membrane.Core.Bin.CallbackContext -end diff --git a/lib/membrane/bin/callback_context/tick.ex b/lib/membrane/bin/callback_context/tick.ex deleted file mode 100644 index 937d44047..000000000 --- a/lib/membrane/bin/callback_context/tick.ex +++ /dev/null @@ -1,7 +0,0 @@ -defmodule Membrane.Bin.CallbackContext.Tick do - @moduledoc """ - Structure representing a context that is passed to the callback when - bin handles timer tick. - """ - use Membrane.Core.Bin.CallbackContext -end diff --git a/lib/membrane/core/bin.ex b/lib/membrane/core/bin.ex index 73a17eb67..b24429c1d 100644 --- a/lib/membrane/core/bin.ex +++ b/lib/membrane/core/bin.ex @@ -125,13 +125,11 @@ defmodule Membrane.Core.Bin do } |> Child.PadSpecHandler.init_pads() - require CallbackContext.Init - state = CallbackHandler.exec_and_handle_callback( :handle_init, ActionHandler, - %{context: &CallbackContext.Init.from_state/1}, + %{context: &CallbackContext.from_state/1}, [], %{state | internal_state: options.user_options} ) diff --git a/lib/membrane/core/bin/callback_context.ex b/lib/membrane/core/bin/callback_context.ex deleted file mode 100644 index dba0b431e..000000000 --- a/lib/membrane/core/bin/callback_context.ex +++ /dev/null @@ -1,29 +0,0 @@ -defmodule Membrane.Core.Bin.CallbackContext do - @moduledoc false - - use Membrane.Core.CallbackContext, - clock: Membrane.Clock.t(), - parent_clock: Membrane.Clock.t(), - pads: %{Membrane.Pad.ref_t() => Membrane.Bin.PadData.t()}, - name: Membrane.Bin.name_t(), - children: %{Membrane.Child.name_t() => Membrane.ChildEntry.t()}, - playback: Membrane.Playback.t(), - resource_guard: Membrane.ResourceGuard.t(), - utility_supervisor: Membrane.UtilitySupervisor.t() - - @impl true - def extract_default_fields(state, args) do - quote do - [ - clock: unquote(state).synchronization.clock, - parent_clock: unquote(state).synchronization.parent_clock, - pads: unquote(state).pads_data, - name: unquote(state).name, - children: unquote(state).children, - playback: unquote(state).playback, - resource_guard: unquote(state).resource_guard, - utility_supervisor: unquote(state).subprocess_supervisor - ] - end ++ args - end -end diff --git a/lib/membrane/core/bin/pad_controller.ex b/lib/membrane/core/bin/pad_controller.ex index 42211b593..29dd30307 100644 --- a/lib/membrane/core/bin/pad_controller.ex +++ b/lib/membrane/core/bin/pad_controller.ex @@ -15,7 +15,7 @@ defmodule Membrane.Core.Bin.PadController do require Membrane.Core.Child.PadModel require Membrane.Core.Message - require Membrane.Bin.CallbackContext.{PadAdded, PadRemoved} + require Membrane.Bin.CallbackContext require Membrane.Logger require Membrane.Pad @@ -278,11 +278,10 @@ defmodule Membrane.Core.Bin.PadController do @spec maybe_handle_pad_added(Pad.ref_t(), Core.Bin.State.t()) :: Core.Bin.State.t() defp maybe_handle_pad_added(ref, state) do - %{options: pad_opts, direction: direction, availability: availability} = - PadModel.get_data!(state, ref) + %{options: pad_opts, availability: availability} = PadModel.get_data!(state, ref) if Pad.availability_mode(availability) == :dynamic do - context = &CallbackContext.PadAdded.from_state(&1, options: pad_opts, direction: direction) + context = &CallbackContext.from_state(&1, pad_options: pad_opts) CallbackHandler.exec_and_handle_callback( :handle_pad_added, @@ -298,15 +297,13 @@ defmodule Membrane.Core.Bin.PadController do @spec maybe_handle_pad_removed(Pad.ref_t(), Core.Bin.State.t()) :: Core.Bin.State.t() defp maybe_handle_pad_removed(ref, state) do - %{direction: direction, availability: availability} = PadModel.get_data!(state, ref) + %{availability: availability} = PadModel.get_data!(state, ref) if Pad.availability_mode(availability) == :dynamic do - context = &CallbackContext.PadRemoved.from_state(&1, direction: direction) - CallbackHandler.exec_and_handle_callback( :handle_pad_removed, ActionHandler, - %{context: context}, + %{context: &CallbackContext.from_state/1}, [ref], state ) diff --git a/lib/membrane/core/callback_context.ex b/lib/membrane/core/callback_context.ex deleted file mode 100644 index 7069be488..000000000 --- a/lib/membrane/core/callback_context.ex +++ /dev/null @@ -1,63 +0,0 @@ -defmodule Membrane.Core.CallbackContext do - @moduledoc false - - @callback extract_default_fields(state :: Macro.t(), args :: keyword(Macro.t())) :: - keyword(Macro.t()) - - defmacro __using__(default_fields) do - quote do - @behaviour unquote(__MODULE__) - - @type default_fields :: %{unquote_splicing(default_fields)} - - @impl true - def extract_default_fields(_state, args) do - args - end - - defoverridable unquote(__MODULE__) - - @macrocallback from_state(state :: Macro.t(), args :: keyword(Macro.t())) :: Macro.t() - - unquote(nested_using({:quote, [], [[do: default_fields]]})) - end - end - - defp nested_using(default_fields) do - quote do - defmacro __using__(fields) do - default_fields = unquote(default_fields) - - quote do - require unquote(__MODULE__) - @behaviour unquote(__MODULE__) - - @type t :: %__MODULE__{unquote_splicing(fields ++ default_fields)} - - fields_names = unquote(Keyword.keys(fields)) - default_fields_names = unquote(Keyword.keys(default_fields)) - - @enforce_keys Module.get_attribute(__MODULE__, :enforce_keys, fields_names) ++ - default_fields_names - - defstruct fields_names ++ default_fields_names - - @impl true - defmacro from_state(state, args \\ []) do - module = unquote(__MODULE__) - - quote do - require unquote(module) - - %unquote(__MODULE__){ - unquote_splicing(module.extract_default_fields(state, args)) - } - end - end - - defoverridable unquote(__MODULE__) - end - end - end - end -end diff --git a/lib/membrane/core/child/lifecycle_controller.ex b/lib/membrane/core/child/lifecycle_controller.ex index ac2357c1f..14f04bc59 100644 --- a/lib/membrane/core/child/lifecycle_controller.ex +++ b/lib/membrane/core/child/lifecycle_controller.ex @@ -7,13 +7,13 @@ defmodule Membrane.Core.Child.LifecycleController do @spec handle_parent_notification(Membrane.ParentNotification.t(), Membrane.Core.Child.state_t()) :: Membrane.Core.Child.state_t() def handle_parent_notification(notification, state) do - context = Component.callback_context_generator(:child, ParentNotification, state) + # context = Component.callback_context(state) action_handler = Component.action_handler(state) CallbackHandler.exec_and_handle_callback( :handle_parent_notification, action_handler, - %{context: context}, + %{context: &Component.callback_context/1}, notification, state ) diff --git a/lib/membrane/core/component.ex b/lib/membrane/core/component.ex index ca1e3b953..df83b960d 100644 --- a/lib/membrane/core/component.ex +++ b/lib/membrane/core/component.ex @@ -6,6 +6,18 @@ defmodule Membrane.Core.Component do | Membrane.Core.Bin.State.t() | Membrane.Core.Element.State.t() + @type callback_context_option_t :: + Membrane.Element.CallbackContext.option_t() + | Membrane.Bin.CallbackContext.option_t() + | Membrane.Pipeline.CallbackContext.option_t() + + @type callback_context_options_t :: [callback_context_option_t()] + + @type callback_context_t :: + Membrane.Element.CallbackContext.t() + | Membrane.Bin.CallbackContext.t() + | Membrane.Pipeline.CallbackContext.t() + @spec action_handler(state_t) :: module [Pipeline, Bin, Element] |> Enum.map(fn component -> @@ -13,40 +25,64 @@ defmodule Membrane.Core.Component do do: unquote(Module.concat([Membrane.Core, component, ActionHandler])) end) - defmacro callback_context_generator(restrict, module, state, args \\ []) do - module = Macro.expand(module, __ENV__) + @spec callback_context(state_t(), callback_context_options_t()) :: + callback_context_t() + # def callback_context(state, args \\ []) - restrict = - case restrict do - :parent -> [Pipeline, Bin] - :child -> [Bin, Element] - :any -> [Pipeline, Bin, Element] - restrict -> restrict + # [Pipeline, Bin, Element] + # |> Enum.map(fn component -> + # def callback_context( + # %unquote(Module.concat([Membrane.Core, component, State])){} = state, + # args + # ), + # do: unquote(Module.concat([Membrane, component, CallbackContext])).from_state(state, args) + # end) + + def callback_context(state, args \\ []) do + callback_context_module = + case state do + %Membrane.Core.Element.State{} -> Membrane.Element.CallbackContext + %Membrane.Core.Bin.State{} -> Membrane.Bin.CallbackContext + %Membrane.Core.Pipeline.State{} -> Membrane.Pipeline.CallbackContext end - requires = - restrict - |> Enum.map(fn component -> - quote do - require unquote(context(component, module)) - end - end) - - clauses = - restrict - |> Enum.flat_map(fn component -> - quote do - %unquote(state(component)){} -> - &unquote(context(component, module)).from_state(&1, unquote(args)) - end - end) - - quote do - unquote_splicing(requires) - unquote({:case, [], [state, [do: clauses]]}) - end + callback_context_module.from_state(state, args) end + # defmacro callback_context_generator(restrict, module, state, args \\ []) do + # module = Macro.expand(module, __ENV__) + + # restrict = + # case restrict do + # :parent -> [Pipeline, Bin] + # :child -> [Bin, Element] + # :any -> [Pipeline, Bin, Element] + # restrict -> restrict + # end + + # requires = + # restrict + # |> Enum.map(fn component -> + # quote do + # require unquote(context(component, module)) + # end + # end) + + # clauses = + # restrict + # |> Enum.flat_map(fn component -> + # quote do + # %unquote(state(component)){} -> + # &unquote(context(component, module)).from_state(&1, unquote(args)) + # end + # end) + + # quote do + # unquote_splicing(requires) + # unquote({:case, [], [state, [do: clauses]]}) + # end + # end + @spec is_pipeline?(state_t) :: boolean() def is_pipeline?(%Membrane.Core.Pipeline.State{}), do: true def is_pipeline?(_state), do: false @@ -65,8 +101,8 @@ defmodule Membrane.Core.Component do @spec is_parent?(state_t) :: boolean() def is_parent?(state), do: is_pipeline?(state) or is_bin?(state) - defp context(component, module), - do: Module.concat([Membrane, component, CallbackContext, module]) + # defp context(component, module), + # do: Module.concat([Membrane, component, CallbackContext, module]) - defp state(component), do: Module.concat([Membrane.Core, component, State]) + # defp state(component), do: Module.concat([Membrane.Core, component, State]) end diff --git a/lib/membrane/core/element/buffer_controller.ex b/lib/membrane/core/element/buffer_controller.ex index 8a787bf6a..2ce5cd0b4 100644 --- a/lib/membrane/core/element/buffer_controller.ex +++ b/lib/membrane/core/element/buffer_controller.ex @@ -89,13 +89,12 @@ defmodule Membrane.Core.Element.BufferController do State.t() ) :: State.t() def exec_buffer_callback(pad_ref, buffers, %State{type: :filter} = state) do - require CallbackContext.Process Telemetry.report_metric("buffer", 1, inspect(pad_ref)) CallbackHandler.exec_and_handle_callback( :handle_process_list, ActionHandler, - %{context: &CallbackContext.Process.from_state/1}, + %{context: &CallbackContext.from_state/1}, [pad_ref, buffers], state ) @@ -103,15 +102,13 @@ defmodule Membrane.Core.Element.BufferController do def exec_buffer_callback(pad_ref, buffers, %State{type: type} = state) when type in [:sink, :endpoint] do - require CallbackContext.Write - Telemetry.report_metric(:buffer, length(List.wrap(buffers))) Telemetry.report_bitrate(buffers) CallbackHandler.exec_and_handle_callback( :handle_write_list, ActionHandler, - %{context: &CallbackContext.Write.from_state/1}, + %{context: &CallbackContext.from_state/1}, [pad_ref, buffers], state ) diff --git a/lib/membrane/core/element/callback_context.ex b/lib/membrane/core/element/callback_context.ex deleted file mode 100644 index 27dae9add..000000000 --- a/lib/membrane/core/element/callback_context.ex +++ /dev/null @@ -1,27 +0,0 @@ -defmodule Membrane.Core.Element.CallbackContext do - @moduledoc false - - use Membrane.Core.CallbackContext, - pads: %{Membrane.Pad.ref_t() => Membrane.Element.PadData.t()}, - clock: Membrane.Clock.t() | nil, - parent_clock: Membrane.Clock.t() | nil, - name: Membrane.Element.name_t(), - playback: Membrane.Playback.t(), - resource_guard: Membrane.ResourceGuard.t(), - utility_supervisor: Membrane.UtilitySupervisor.t() - - @impl true - def extract_default_fields(state, args) do - quote do - [ - pads: unquote(state).pads_data, - clock: unquote(state).synchronization.clock, - parent_clock: unquote(state).synchronization.parent_clock, - name: unquote(state).name, - playback: unquote(state).playback, - resource_guard: unquote(state).resource_guard, - utility_supervisor: unquote(state).subprocess_supervisor - ] - end ++ args - end -end diff --git a/lib/membrane/core/element/demand_controller.ex b/lib/membrane/core/element/demand_controller.ex index 721bb8184..484251938 100644 --- a/lib/membrane/core/element/demand_controller.ex +++ b/lib/membrane/core/element/demand_controller.ex @@ -11,7 +11,6 @@ defmodule Membrane.Core.Element.DemandController do alias Membrane.Element.CallbackContext alias Membrane.Pad - require CallbackContext.Demand require Membrane.Core.Child.PadModel require Membrane.Logger @@ -51,8 +50,7 @@ defmodule Membrane.Core.Element.DemandController do state = PadModel.set_data!(state, pad_ref, data) if exec_handle_demand?(data) do - require CallbackContext.Demand - context = &CallbackContext.Demand.from_state(&1, incoming_demand: size) + context = &CallbackContext.from_state(&1, incoming_demand: size) CallbackHandler.exec_and_handle_callback( :handle_demand, diff --git a/lib/membrane/core/element/demand_handler.ex b/lib/membrane/core/element/demand_handler.ex index 6e749ed8d..4ca8879d1 100644 --- a/lib/membrane/core/element/demand_handler.ex +++ b/lib/membrane/core/element/demand_handler.ex @@ -195,8 +195,8 @@ defmodule Membrane.Core.Element.DemandHandler do defp do_handle_input_queue_output(pad_ref, {:event, e}, state), do: EventController.exec_handle_event(pad_ref, e, state) - defp do_handle_input_queue_output(pad_ref, {:stream_format, c}, state), - do: StreamFormatController.exec_handle_stream_format(pad_ref, c, state) + defp do_handle_input_queue_output(pad_ref, {:stream_format, stream_format}, state), + do: StreamFormatController.exec_handle_stream_format(pad_ref, stream_format, state) defp do_handle_input_queue_output( pad_ref, diff --git a/lib/membrane/core/element/event_controller.ex b/lib/membrane/core/element/event_controller.ex index 7a04f1825..abaf82600 100644 --- a/lib/membrane/core/element/event_controller.ex +++ b/lib/membrane/core/element/event_controller.ex @@ -68,8 +68,7 @@ defmodule Membrane.Core.Element.EventController do defp do_exec_handle_event(pad_ref, %event_type{} = event, params, state) when event_type in [Events.StartOfStream, Events.EndOfStream] do data = PadModel.get_data!(state, pad_ref) - require CallbackContext.StreamManagement - context = CallbackContext.StreamManagement.from_state(state) + context = CallbackContext.from_state(state) callback = stream_event_to_callback(event) new_params = Map.put(params, :direction, data.direction) @@ -95,9 +94,10 @@ defmodule Membrane.Core.Element.EventController do defp do_exec_handle_event(pad_ref, event, params, state) do data = PadModel.get_data!(state, pad_ref) - require CallbackContext.Event - context = &CallbackContext.Event.from_state/1 - params = %{context: context, direction: data.direction} |> Map.merge(params) + + params = + %{context: &CallbackContext.from_state/1, direction: data.direction} |> Map.merge(params) + args = [pad_ref, event] CallbackHandler.exec_and_handle_callback(:handle_event, ActionHandler, params, args, state) end diff --git a/lib/membrane/core/element/lifecycle_controller.ex b/lib/membrane/core/element/lifecycle_controller.ex index fc55130ff..ced076bd6 100644 --- a/lib/membrane/core/element/lifecycle_controller.ex +++ b/lib/membrane/core/element/lifecycle_controller.ex @@ -36,13 +36,12 @@ defmodule Membrane.Core.Element.LifecycleController do state = put_in(state.synchronization.clock, clock) Message.send(state.parent_pid, :clock, [state.name, clock]) - require CallbackContext.Init state = CallbackHandler.exec_and_handle_callback( :handle_init, ActionHandler, - %{context: &CallbackContext.Init.from_state/1}, + %{context: &CallbackContext.from_state/1}, [], %{state | internal_state: options} ) @@ -52,14 +51,11 @@ defmodule Membrane.Core.Element.LifecycleController do @spec handle_setup(State.t()) :: State.t() def handle_setup(state) do - require CallbackContext.Setup - context = &CallbackContext.Setup.from_state/1 - state = CallbackHandler.exec_and_handle_callback( :handle_setup, ActionHandler, - %{context: context}, + %{context: &CallbackContext.from_state/1}, [], state ) @@ -75,14 +71,12 @@ defmodule Membrane.Core.Element.LifecycleController do Membrane.Logger.debug("Got play request") state = %State{state | playback: :playing} - require CallbackContext.Playing - context = &CallbackContext.Playing.from_state/1 state = CallbackHandler.exec_and_handle_callback( :handle_playing, ActionHandler, - %{context: context}, + %{context: &CallbackContext.from_state/1}, [], state ) @@ -104,14 +98,11 @@ defmodule Membrane.Core.Element.LifecycleController do state = %{state | terminating?: true} - require CallbackContext.TerminateRequest - context = &CallbackContext.TerminateRequest.from_state/1 - state = CallbackHandler.exec_and_handle_callback( :handle_terminate_request, ActionHandler, - %{context: context}, + %{context: &CallbackContext.from_state/1}, [], state ) @@ -124,13 +115,10 @@ defmodule Membrane.Core.Element.LifecycleController do """ @spec handle_info(message :: any, State.t()) :: State.t() def handle_info(message, state) do - require CallbackContext.Info - context = &CallbackContext.Info.from_state/1 - CallbackHandler.exec_and_handle_callback( :handle_info, ActionHandler, - %{context: context}, + %{context: &CallbackContext.from_state/1}, [message], state ) diff --git a/lib/membrane/core/element/pad_controller.ex b/lib/membrane/core/element/pad_controller.ex index 8c4883282..525ef4b11 100644 --- a/lib/membrane/core/element/pad_controller.ex +++ b/lib/membrane/core/element/pad_controller.ex @@ -23,7 +23,7 @@ defmodule Membrane.Core.Element.PadController do require Membrane.Core.Child.PadModel require Membrane.Core.Message - require Membrane.Element.CallbackContext.{PadAdded, PadRemoved} + require Membrane.Element.CallbackContext require Membrane.Logger require Membrane.Pad @@ -391,11 +391,10 @@ defmodule Membrane.Core.Element.PadController do @spec maybe_handle_pad_added(Pad.ref_t(), State.t()) :: State.t() defp maybe_handle_pad_added(ref, state) do - %{options: pad_opts, direction: direction, availability: availability} = - PadModel.get_data!(state, ref) + %{options: pad_opts, availability: availability} = PadModel.get_data!(state, ref) if Pad.availability_mode(availability) == :dynamic do - context = &CallbackContext.PadAdded.from_state(&1, options: pad_opts, direction: direction) + context = &CallbackContext.from_state(&1, pad_options: pad_opts) CallbackHandler.exec_and_handle_callback( :handle_pad_added, @@ -411,15 +410,13 @@ defmodule Membrane.Core.Element.PadController do @spec maybe_handle_pad_removed(Pad.ref_t(), State.t()) :: State.t() defp maybe_handle_pad_removed(ref, state) do - %{direction: direction, availability: availability} = PadModel.get_data!(state, ref) + %{availability: availability} = PadModel.get_data!(state, ref) if Pad.availability_mode(availability) == :dynamic do - context = &CallbackContext.PadRemoved.from_state(&1, direction: direction) - CallbackHandler.exec_and_handle_callback( :handle_pad_removed, ActionHandler, - %{context: context}, + %{context: &CallbackContext.from_state/1}, [ref], state ) diff --git a/lib/membrane/core/element/stream_format_controller.ex b/lib/membrane/core/element/stream_format_controller.ex index 4697fc09d..f33befeeb 100644 --- a/lib/membrane/core/element/stream_format_controller.ex +++ b/lib/membrane/core/element/stream_format_controller.ex @@ -53,12 +53,13 @@ defmodule Membrane.Core.Element.StreamFormatController do @spec exec_handle_stream_format(Pad.ref_t(), StreamFormat.t(), params :: map, State.t()) :: State.t() def exec_handle_stream_format(pad_ref, stream_format, params \\ %{}, state) do - require CallbackContext.StreamFormat + %{ + stream_format_validation_params: stream_format_validation_params, + name: pad_name, + stream_format: old_stream_format + } = PadModel.get_data!(state, pad_ref) - %{stream_format_validation_params: stream_format_validation_params, name: pad_name} = - PadModel.get_data!(state, pad_ref) - - context = &CallbackContext.StreamFormat.from_state(&1, pad: pad_ref) + context = &CallbackContext.from_state(&1, old_stream_format: old_stream_format) :ok = validate_stream_format!( diff --git a/lib/membrane/core/filter_aggregator/context.ex b/lib/membrane/core/filter_aggregator/context.ex index 880c5d265..0a96c262e 100644 --- a/lib/membrane/core/filter_aggregator/context.ex +++ b/lib/membrane/core/filter_aggregator/context.ex @@ -5,7 +5,7 @@ defmodule Membrane.Core.FilterAggregator.Context do require Membrane.Core.FilterAggregator.InternalAction, as: InternalAction - @type t :: Membrane.Core.Element.CallbackContext.default_fields() + @type t :: Element.CallbackContext.t() @typedoc """ Collection of states for encapsuled elements as kept in `Membrane.FilterAggregator` element diff --git a/lib/membrane/core/parent/child_life_controller.ex b/lib/membrane/core/parent/child_life_controller.ex index 416404116..090d0f077 100644 --- a/lib/membrane/core/parent/child_life_controller.ex +++ b/lib/membrane/core/parent/child_life_controller.ex @@ -635,8 +635,8 @@ defmodule Membrane.Core.Parent.ChildLifeController do crash_initiator, state ) do - context = - Component.callback_context_generator(:parent, CrashGroupDown, state, + context_generator = + &Component.callback_context(&1, members: group_members, crash_initiator: crash_initiator ) @@ -644,7 +644,7 @@ defmodule Membrane.Core.Parent.ChildLifeController do CallbackHandler.exec_and_handle_callback( :handle_crash_group_down, Membrane.Core.Pipeline.ActionHandler, - %{context: context}, + %{context: context_generator}, [group_name], state ) diff --git a/lib/membrane/core/parent/child_life_controller/startup_utils.ex b/lib/membrane/core/parent/child_life_controller/startup_utils.ex index f1eb2e8e1..2c9c7ba3d 100644 --- a/lib/membrane/core/parent/child_life_controller/startup_utils.ex +++ b/lib/membrane/core/parent/child_life_controller/startup_utils.ex @@ -106,7 +106,7 @@ defmodule Membrane.Core.Parent.ChildLifeController.StartupUtils do @spec exec_handle_spec_started([Membrane.Child.name_t()], Parent.state_t()) :: Parent.state_t() def exec_handle_spec_started(children_names, state) do - context = Component.callback_context_generator(:parent, SpecStarted, state) + # context = Component.callback_context(state) action_handler = case state do @@ -117,7 +117,7 @@ defmodule Membrane.Core.Parent.ChildLifeController.StartupUtils do CallbackHandler.exec_and_handle_callback( :handle_spec_started, action_handler, - %{context: context}, + %{context: &Component.callback_context/1}, [children_names], state ) diff --git a/lib/membrane/core/parent/lifecycle_controller.ex b/lib/membrane/core/parent/lifecycle_controller.ex index f2c826ec1..002288849 100644 --- a/lib/membrane/core/parent/lifecycle_controller.ex +++ b/lib/membrane/core/parent/lifecycle_controller.ex @@ -22,13 +22,13 @@ defmodule Membrane.Core.Parent.LifecycleController do @spec handle_setup(Parent.state_t()) :: Parent.state_t() def handle_setup(state) do Membrane.Logger.debug("Setup") - context = Component.callback_context_generator(:parent, Setup, state) + # context = Component.callback_context(state) state = CallbackHandler.exec_and_handle_callback( :handle_setup, Component.action_handler(state), - %{context: context}, + %{context: &Component.callback_context/1}, [], state ) @@ -53,12 +53,12 @@ defmodule Membrane.Core.Parent.LifecycleController do end) state = %{state | playback: :playing} - context = Component.callback_context_generator(:parent, Playing, state) + # context = Component.callback_context(state) CallbackHandler.exec_and_handle_callback( :handle_playing, Component.action_handler(state), - %{context: context}, + %{context: &Component.callback_context/1}, [], state ) @@ -71,12 +71,12 @@ defmodule Membrane.Core.Parent.LifecycleController do def handle_terminate_request(state) do state = %{state | terminating?: true} - context = Component.callback_context_generator(:parent, TerminateRequest, state) + # context = Component.callback_context(state) CallbackHandler.exec_and_handle_callback( :handle_terminate_request, Component.action_handler(state), - %{context: context}, + %{context: &Component.callback_context/1}, [], state ) @@ -114,12 +114,12 @@ defmodule Membrane.Core.Parent.LifecycleController do ) Parent.ChildrenModel.assert_child_exists!(state, from) - context = Component.callback_context_generator(:parent, ChildNotification, state) + # context = Component.callback_context(state) CallbackHandler.exec_and_handle_callback( :handle_child_notification, Component.action_handler(state), - %{context: context}, + %{context: &Component.callback_context/1}, [notification, from], state ) @@ -127,12 +127,12 @@ defmodule Membrane.Core.Parent.LifecycleController do @spec handle_info(any, Parent.state_t()) :: Parent.state_t() def handle_info(message, state) do - context = Component.callback_context_generator(:parent, Info, state) + # context = Component.callback_context(state) CallbackHandler.exec_and_handle_callback( :handle_info, Component.action_handler(state), - %{context: context}, + %{context: &Component.callback_context/1}, [message], state ) @@ -146,7 +146,7 @@ defmodule Membrane.Core.Parent.LifecycleController do ) :: Parent.state_t() def handle_stream_management_event(%event_type{}, element_name, pad_ref, state) when event_type in [Events.StartOfStream, Events.EndOfStream] do - context = Component.callback_context_generator(:parent, StreamManagement, state) + # context = Component.callback_context(state) callback = case event_type do @@ -157,7 +157,7 @@ defmodule Membrane.Core.Parent.LifecycleController do CallbackHandler.exec_and_handle_callback( callback, Component.action_handler(state), - %{context: context}, + %{context: &Component.callback_context/1}, [element_name, pad_ref], state ) diff --git a/lib/membrane/core/pipeline.ex b/lib/membrane/core/pipeline.ex index a019d91ff..8a3780ea4 100644 --- a/lib/membrane/core/pipeline.ex +++ b/lib/membrane/core/pipeline.ex @@ -13,7 +13,6 @@ defmodule Membrane.Core.Pipeline do require Membrane.Core.Telemetry, as: Telemetry require Membrane.Logger require Membrane.Core.Component - require Membrane.Pipeline.CallbackContext.Call @impl GenServer def init(params) do @@ -48,13 +47,11 @@ defmodule Membrane.Core.Pipeline do resource_guard: resource_guard } - require CallbackContext.Init - state = CallbackHandler.exec_and_handle_callback( :handle_init, ActionHandler, - %{context: &CallbackContext.Init.from_state/1}, + %{context: &CallbackContext.from_state/1}, [], %{state | internal_state: params.options} ) @@ -147,7 +144,7 @@ defmodule Membrane.Core.Pipeline do @impl GenServer def handle_call(message, from, state) do - context = &CallbackContext.Call.from_state(&1, from: from) + context = &CallbackContext.from_state(&1, from: from) CallbackHandler.exec_and_handle_callback( :handle_call, diff --git a/lib/membrane/core/pipeline/callback_context.ex b/lib/membrane/core/pipeline/callback_context.ex deleted file mode 100644 index 49f942553..000000000 --- a/lib/membrane/core/pipeline/callback_context.ex +++ /dev/null @@ -1,23 +0,0 @@ -defmodule Membrane.Core.Pipeline.CallbackContext do - @moduledoc false - - use Membrane.Core.CallbackContext, - clock: Membrane.Clock.t(), - children: %{Membrane.Child.name_t() => Membrane.ChildEntry.t()}, - playback: Membrane.Playback.t(), - resource_guard: Membrane.ResourceGuard.t(), - utility_supervisor: Membrane.UtilitySupervisor.t() - - @impl true - def extract_default_fields(state, args) do - quote do - [ - clock: unquote(state).synchronization.clock_proxy, - children: unquote(state).children, - playback: unquote(state).playback, - resource_guard: unquote(state).resource_guard, - utility_supervisor: unquote(state).subprocess_supervisor - ] - end ++ args - end -end diff --git a/lib/membrane/core/timer_controller.ex b/lib/membrane/core/timer_controller.ex index 612ac03b9..de4928e7d 100644 --- a/lib/membrane/core/timer_controller.ex +++ b/lib/membrane/core/timer_controller.ex @@ -5,7 +5,6 @@ defmodule Membrane.Core.TimerController do alias Membrane.Core.{CallbackHandler, Component, Timer} require Membrane.Core.Component - require Membrane.Element.CallbackContext.Tick defguardp is_timer_present(timer_id, state) when is_map_key(state.synchronization.timers, timer_id) @@ -59,13 +58,13 @@ defmodule Membrane.Core.TimerController do @spec handle_tick(Timer.id_t(), Component.state_t()) :: Component.state_t() def handle_tick(timer_id, state) when is_timer_present(timer_id, state) do - context = Component.callback_context_generator(:any, Tick, state) + # context = Component.callback_context(state) state = CallbackHandler.exec_and_handle_callback( :handle_tick, Component.action_handler(state), - %{context: context}, + %{context: &Component.callback_context/1}, [timer_id], state ) diff --git a/lib/membrane/element/base.ex b/lib/membrane/element/base.ex index 1aeed4e50..7c684a835 100644 --- a/lib/membrane/element/base.ex +++ b/lib/membrane/element/base.ex @@ -65,7 +65,7 @@ defmodule Membrane.Element.Base do For these reasons, it's important to do any long-lasting or complex work in `c:handle_setup/2`, while `handle_init` should be used for things like parsing options or initializing state. """ - @callback handle_init(context :: CallbackContext.Init.t(), options :: Element.options_t()) :: + @callback handle_init(context :: CallbackContext.t(), options :: Element.options_t()) :: callback_return_t @doc """ @@ -74,7 +74,7 @@ defmodule Membrane.Element.Base do Any long-lasting or complex initialization should happen here. """ @callback handle_setup( - context :: CallbackContext.Setup.t(), + context :: CallbackContext.t(), state :: Element.state_t() ) :: callback_return_t @@ -85,7 +85,7 @@ defmodule Membrane.Element.Base do through its pads. """ @callback handle_playing( - context :: CallbackContext.Playing.t(), + context :: CallbackContext.t(), state :: Element.state_t() ) :: callback_return_t @@ -97,7 +97,7 @@ defmodule Membrane.Element.Base do """ @callback handle_info( message :: any(), - context :: CallbackContext.Info.t(), + context :: CallbackContext.t(), state :: Element.state_t() ) :: callback_return_t @@ -107,7 +107,7 @@ defmodule Membrane.Element.Base do """ @callback handle_pad_added( pad :: Pad.ref_t(), - context :: CallbackContext.PadAdded.t(), + context :: CallbackContext.t(), state :: Element.state_t() ) :: callback_return_t @@ -117,7 +117,7 @@ defmodule Membrane.Element.Base do """ @callback handle_pad_removed( pad :: Pad.ref_t(), - context :: CallbackContext.PadRemoved.t(), + context :: CallbackContext.t(), state :: Element.state_t() ) :: callback_return_t @@ -130,7 +130,7 @@ defmodule Membrane.Element.Base do @callback handle_event( pad :: Pad.ref_t(), event :: Event.t(), - context :: CallbackContext.Event.t(), + context :: CallbackContext.t(), state :: Element.state_t() ) :: callback_return_t @@ -140,7 +140,7 @@ defmodule Membrane.Element.Base do """ @callback handle_tick( timer_id :: any, - context :: CallbackContext.Tick.t(), + context :: CallbackContext.t(), state :: Element.state_t() ) :: callback_return_t @@ -149,7 +149,7 @@ defmodule Membrane.Element.Base do """ @callback handle_parent_notification( notification :: Membrane.ParentNotification.t(), - context :: Membrane.Element.CallbackContext.ParentNotification.t(), + context :: CallbackContext.t(), state :: Element.state_t() ) :: callback_return_t @@ -159,7 +159,7 @@ defmodule Membrane.Element.Base do By default it returns `t:Membrane.Element.Action.terminate_t/0` with reason `:normal`. """ @callback handle_terminate_request( - context :: CallbackContext.TerminateRequest.t(), + context :: CallbackContext.t(), state :: Element.state_t() ) :: callback_return_t() diff --git a/lib/membrane/element/callback_context.ex b/lib/membrane/element/callback_context.ex new file mode 100644 index 000000000..b91e16576 --- /dev/null +++ b/lib/membrane/element/callback_context.ex @@ -0,0 +1,37 @@ +defmodule Membrane.Element.CallbackContext do + @moduledoc false + + @type t :: %{ + :pads => %{Membrane.Pad.ref_t() => Membrane.Element.PadData.t()}, + :clock => Membrane.Clock.t() | nil, + :parent_clock => Membrane.Clock.t() | nil, + :name => Membrane.Element.name_t(), + :playback => Membrane.Playback.t(), + :resource_guard => Membrane.ResourceGuard.t(), + :utility_supervisor => Membrane.UtilitySupervisor.t(), + optional(:incoming_demand) => non_neg_integer(), + optional(:pad_options) => map(), + optional(:old_stream_format) => Membrane.StreamFormat.t() + } + + @type option_t :: + {:incoming_demand, non_neg_integer()} + | {:pad_options, map()} + | {:old_stream_format, Membrane.StreamFormat.t()} + + @type options_t :: [option_t()] + + @spec from_state(Membrane.Core.Element.State.t(), options_t()) :: t() + def from_state(state, additional_fields \\ []) do + Map.new(additional_fields) + |> Map.merge(%{ + pads: state.pads_data, + clock: state.synchronization.clock, + parent_clock: state.synchronization.parent_clock, + name: state.name, + playback: state.playback, + resource_guard: state.resource_guard, + utility_supervisor: state.subprocess_supervisor + }) + end +end diff --git a/lib/membrane/element/callback_context/demand.ex b/lib/membrane/element/callback_context/demand.ex deleted file mode 100644 index 0cf730c1a..000000000 --- a/lib/membrane/element/callback_context/demand.ex +++ /dev/null @@ -1,8 +0,0 @@ -defmodule Membrane.Element.CallbackContext.Demand do - @moduledoc """ - Structure representing a context that is passed to the element - when processing incoming demand. - """ - use Membrane.Core.Element.CallbackContext, - incoming_demand: non_neg_integer() -end diff --git a/lib/membrane/element/callback_context/event.ex b/lib/membrane/element/callback_context/event.ex deleted file mode 100644 index 91f46a1cb..000000000 --- a/lib/membrane/element/callback_context/event.ex +++ /dev/null @@ -1,7 +0,0 @@ -defmodule Membrane.Element.CallbackContext.Event do - @moduledoc """ - Structure representing a context that is passed to the element - when handling event. - """ - use Membrane.Core.Element.CallbackContext -end diff --git a/lib/membrane/element/callback_context/info.ex b/lib/membrane/element/callback_context/info.ex deleted file mode 100644 index 15147925b..000000000 --- a/lib/membrane/element/callback_context/info.ex +++ /dev/null @@ -1,7 +0,0 @@ -defmodule Membrane.Element.CallbackContext.Info do - @moduledoc """ - Structure representing a context that is passed to the callback when - element receives unrecognized message. - """ - use Membrane.Core.Element.CallbackContext -end diff --git a/lib/membrane/element/callback_context/init.ex b/lib/membrane/element/callback_context/init.ex deleted file mode 100644 index 69b289869..000000000 --- a/lib/membrane/element/callback_context/init.ex +++ /dev/null @@ -1,6 +0,0 @@ -defmodule Membrane.Element.CallbackContext.Init do - @moduledoc """ - Callback context for `c:Membrane.Element.Base.handle_init/2`. - """ - use Membrane.Core.Element.CallbackContext -end diff --git a/lib/membrane/element/callback_context/pad_added.ex b/lib/membrane/element/callback_context/pad_added.ex deleted file mode 100644 index 2cd3a923e..000000000 --- a/lib/membrane/element/callback_context/pad_added.ex +++ /dev/null @@ -1,9 +0,0 @@ -defmodule Membrane.Element.CallbackContext.PadAdded do - @moduledoc """ - Structure representing a context that is passed to the element - when a new dynamic pad instance is created - """ - use Membrane.Core.Element.CallbackContext, - direction: :input | :output, - options: map() -end diff --git a/lib/membrane/element/callback_context/pad_removed.ex b/lib/membrane/element/callback_context/pad_removed.ex deleted file mode 100644 index b25a534bc..000000000 --- a/lib/membrane/element/callback_context/pad_removed.ex +++ /dev/null @@ -1,8 +0,0 @@ -defmodule Membrane.Element.CallbackContext.PadRemoved do - @moduledoc """ - Structure representing a context that is passed to the element - when a dynamic pad is removed - """ - use Membrane.Core.Element.CallbackContext, - direction: :input | :output -end diff --git a/lib/membrane/element/callback_context/parent_notification.ex b/lib/membrane/element/callback_context/parent_notification.ex deleted file mode 100644 index d9e9fa15a..000000000 --- a/lib/membrane/element/callback_context/parent_notification.ex +++ /dev/null @@ -1,7 +0,0 @@ -defmodule Membrane.Element.CallbackContext.ParentNotification do - @moduledoc """ - Structure representing a context that is passed to the callback when - element receives parent notification. - """ - use Membrane.Core.Element.CallbackContext -end diff --git a/lib/membrane/element/callback_context/playing.ex b/lib/membrane/element/callback_context/playing.ex deleted file mode 100644 index 7ef0072b4..000000000 --- a/lib/membrane/element/callback_context/playing.ex +++ /dev/null @@ -1,6 +0,0 @@ -defmodule Membrane.Element.CallbackContext.Playing do - @moduledoc """ - Structure representing a context that is passed to the `c:Membrane.Element.Base.handle_playing/2` callback. - """ - use Membrane.Core.Element.CallbackContext -end diff --git a/lib/membrane/element/callback_context/process.ex b/lib/membrane/element/callback_context/process.ex deleted file mode 100644 index 27c590d89..000000000 --- a/lib/membrane/element/callback_context/process.ex +++ /dev/null @@ -1,6 +0,0 @@ -defmodule Membrane.Element.CallbackContext.Process do - @moduledoc """ - Structure representing a context that is passed to the element when new buffer arrives. - """ - use Membrane.Core.Element.CallbackContext -end diff --git a/lib/membrane/element/callback_context/setup.ex b/lib/membrane/element/callback_context/setup.ex deleted file mode 100644 index 0026c6c0c..000000000 --- a/lib/membrane/element/callback_context/setup.ex +++ /dev/null @@ -1,6 +0,0 @@ -defmodule Membrane.Element.CallbackContext.Setup do - @moduledoc """ - Structure representing a context that is passed to the `c:Membrane.Element.Base.handle_setup/2` callback. - """ - use Membrane.Core.Element.CallbackContext -end diff --git a/lib/membrane/element/callback_context/stream_format.ex b/lib/membrane/element/callback_context/stream_format.ex deleted file mode 100644 index aab6ee4dd..000000000 --- a/lib/membrane/element/callback_context/stream_format.ex +++ /dev/null @@ -1,26 +0,0 @@ -defmodule Membrane.Element.CallbackContext.StreamFormat do - @moduledoc """ - Structure representing a context that is passed to the element when receiving - information about new stream format for given pad. - - The `old_stream_format` field contains stream format previously present on the pad, and is equal - to `pads[pad].stream_format` field. - """ - - use Membrane.Core.Element.CallbackContext, - old_stream_format: Membrane.StreamFormat.t() - - alias Membrane.Core.Child.PadModel - - @impl true - defmacro from_state(state, args) do - {pad, args} = args |> Keyword.pop(:pad) - - old_stream_format = - quote do - unquote(state) |> PadModel.get_data!(unquote(pad), :stream_format) - end - - super(state, args ++ [old_stream_format: old_stream_format]) - end -end diff --git a/lib/membrane/element/callback_context/stream_management.ex b/lib/membrane/element/callback_context/stream_management.ex deleted file mode 100644 index 7f3c03385..000000000 --- a/lib/membrane/element/callback_context/stream_management.ex +++ /dev/null @@ -1,7 +0,0 @@ -defmodule Membrane.Element.CallbackContext.StreamManagement do - @moduledoc """ - Structure representing a context that is passed to the element - when handling start and end of stream events. - """ - use Membrane.Core.Element.CallbackContext -end diff --git a/lib/membrane/element/callback_context/terminate_request.ex b/lib/membrane/element/callback_context/terminate_request.ex deleted file mode 100644 index 1dc654c23..000000000 --- a/lib/membrane/element/callback_context/terminate_request.ex +++ /dev/null @@ -1,6 +0,0 @@ -defmodule Membrane.Element.CallbackContext.TerminateRequest do - @moduledoc """ - Callback context for `c:Membrane.Element.Base.handle_terminate_request/2`. - """ - use Membrane.Core.Element.CallbackContext -end diff --git a/lib/membrane/element/callback_context/tick.ex b/lib/membrane/element/callback_context/tick.ex deleted file mode 100644 index d8b314dd4..000000000 --- a/lib/membrane/element/callback_context/tick.ex +++ /dev/null @@ -1,7 +0,0 @@ -defmodule Membrane.Element.CallbackContext.Tick do - @moduledoc """ - Structure representing a context that is passed to the callback when - element handles timer tick. - """ - use Membrane.Core.Element.CallbackContext -end diff --git a/lib/membrane/element/callback_context/write.ex b/lib/membrane/element/callback_context/write.ex deleted file mode 100644 index 2be481ad5..000000000 --- a/lib/membrane/element/callback_context/write.ex +++ /dev/null @@ -1,7 +0,0 @@ -defmodule Membrane.Element.CallbackContext.Write do - @moduledoc """ - Structure representing a context that is passed to the element - when new buffer arrives to the sink or the endpoint. - """ - use Membrane.Core.Element.CallbackContext -end diff --git a/lib/membrane/element/with_input_pads.ex b/lib/membrane/element/with_input_pads.ex index 2df092840..d805e4926 100644 --- a/lib/membrane/element/with_input_pads.ex +++ b/lib/membrane/element/with_input_pads.ex @@ -21,7 +21,7 @@ defmodule Membrane.Element.WithInputPads do @callback handle_stream_format( pad :: Pad.ref_t(), stream_format :: Membrane.StreamFormat.t(), - context :: CallbackContext.StreamFormat.t(), + context :: CallbackContext.t(), state :: Element.state_t() ) :: Membrane.Element.Base.callback_return_t() @@ -32,7 +32,7 @@ defmodule Membrane.Element.WithInputPads do """ @callback handle_start_of_stream( pad :: Pad.ref_t(), - context :: CallbackContext.StreamManagement.t(), + context :: CallbackContext.t(), state :: Element.state_t() ) :: Membrane.Element.Base.callback_return_t() @@ -42,7 +42,7 @@ defmodule Membrane.Element.WithInputPads do """ @callback handle_end_of_stream( pad :: Pad.ref_t(), - context :: CallbackContext.StreamManagement.t(), + context :: CallbackContext.t(), state :: Element.state_t() ) :: Membrane.Element.Base.callback_return_t() diff --git a/lib/membrane/element/with_output_pads.ex b/lib/membrane/element/with_output_pads.ex index 8ea9ad4d2..ea46a9199 100644 --- a/lib/membrane/element/with_output_pads.ex +++ b/lib/membrane/element/with_output_pads.ex @@ -33,7 +33,7 @@ defmodule Membrane.Element.WithOutputPads do pad :: Pad.ref_t(), size :: non_neg_integer, unit :: Buffer.Metric.unit_t(), - context :: CallbackContext.Demand.t(), + context :: CallbackContext.t(), state :: Element.state_t() ) :: Membrane.Element.Base.callback_return_t() diff --git a/lib/membrane/endpoint.ex b/lib/membrane/endpoint.ex index 9a1e1d6d0..b4bee0772 100644 --- a/lib/membrane/endpoint.ex +++ b/lib/membrane/endpoint.ex @@ -35,7 +35,7 @@ defmodule Membrane.Endpoint do @callback handle_write_list( pad :: Pad.ref_t(), buffers :: list(Buffer.t()), - context :: CallbackContext.Process.t(), + context :: CallbackContext.t(), state :: Element.state_t() ) :: Membrane.Element.Base.callback_return_t() @@ -48,7 +48,7 @@ defmodule Membrane.Endpoint do @callback handle_write( pad :: Pad.ref_t(), buffer :: Buffer.t(), - context :: CallbackContext.Process.t(), + context :: CallbackContext.t(), state :: Element.state_t() ) :: Membrane.Element.Base.callback_return_t() diff --git a/lib/membrane/filter.ex b/lib/membrane/filter.ex index 60bed8e61..276802799 100644 --- a/lib/membrane/filter.ex +++ b/lib/membrane/filter.ex @@ -34,7 +34,7 @@ defmodule Membrane.Filter do @callback handle_process_list( pad :: Pad.ref_t(), buffers :: list(Buffer.t()), - context :: CallbackContext.Process.t(), + context :: CallbackContext.t(), state :: Element.state_t() ) :: Membrane.Element.Base.callback_return_t() @@ -47,7 +47,7 @@ defmodule Membrane.Filter do @callback handle_process( pad :: Pad.ref_t(), buffer :: Buffer.t(), - context :: CallbackContext.Process.t(), + context :: CallbackContext.t(), state :: Element.state_t() ) :: Membrane.Element.Base.callback_return_t() diff --git a/lib/membrane/filter_aggregator.ex b/lib/membrane/filter_aggregator.ex index 942b22231..b75e5e56d 100644 --- a/lib/membrane/filter_aggregator.ex +++ b/lib/membrane/filter_aggregator.ex @@ -197,36 +197,30 @@ defmodule Membrane.FilterAggregator do end defp perform_action({:buffer, {:output, buffer}}, module, context, state) do - cb_context = struct!(CallbackContext.Process, context) - module.handle_process_list(:input, List.wrap(buffer), cb_context, state) + module.handle_process_list(:input, List.wrap(buffer), context, state) end defp perform_action({:stream_format, {:output, stream_format}}, module, context, state) do cb_context = context |> Map.put(:old_stream_format, context.pads.input.stream_format) - |> then(&struct!(CallbackContext.StreamFormat, &1)) module.handle_stream_format(:input, stream_format, cb_context, state) end defp perform_action({:event, {:output, event}}, module, context, state) do - cb_context = struct!(CallbackContext.Event, context) - module.handle_event(:input, event, cb_context, state) + module.handle_event(:input, event, context, state) end # Internal, FilterAggregator action used to trigger handle_start_of_stream defp perform_action(InternalAction.start_of_stream(:output), module, context, state) do - cb_context = struct!(CallbackContext.StreamManagement, context) - - {actions, new_state} = module.handle_start_of_stream(:input, cb_context, state) + {actions, new_state} = module.handle_start_of_stream(:input, context, state) {[InternalAction.start_of_stream(:output) | actions], new_state} end defp perform_action({:end_of_stream, :output}, module, context, state) do - cb_context = struct!(CallbackContext.StreamManagement, context) - module.handle_end_of_stream(:input, cb_context, state) + module.handle_end_of_stream(:input, context, state) end defp perform_action({:demand, {:input, _size}}, _module, _context, _state) do @@ -256,9 +250,8 @@ defmodule Membrane.FilterAggregator do args_lists |> Enum.flat_map_reduce({context, state}, fn [:input, buffer], {acc_context, acc_state} -> acc_context = Context.before_incoming_action(acc_context, {:buffer, {:output, buffer}}) - cb_context = struct!(CallbackContext.Process, acc_context) - {actions, state} = module.handle_process(:input, buffer, cb_context, acc_state) + {actions, state} = module.handle_process(:input, buffer, context, acc_state) acc_context = Context.after_incoming_action(acc_context, {:buffer, {:output, buffer}}) @@ -269,14 +262,12 @@ defmodule Membrane.FilterAggregator do end defp perform_action(InternalAction.setup(), module, context, state) do - cb_context = struct!(CallbackContext.Setup, context) - {actions, state} = module.handle_setup(cb_context, state) + {actions, state} = module.handle_setup(context, state) {actions ++ [InternalAction.setup()], state} end defp perform_action(InternalAction.playing(), module, context, state) do - cb_context = struct!(CallbackContext.Playing, context) - {actions, state} = module.handle_playing(cb_context, state) + {actions, state} = module.handle_playing(context, state) {actions ++ [InternalAction.playing()], state} end diff --git a/lib/membrane/pipeline.ex b/lib/membrane/pipeline.ex index 15e4a12d9..928b399f9 100644 --- a/lib/membrane/pipeline.ex +++ b/lib/membrane/pipeline.ex @@ -104,7 +104,7 @@ defmodule Membrane.Pipeline do while `handle_init` should be used for things like parsing options, initializing state or spawning children. """ - @callback handle_init(context :: CallbackContext.Init.t(), options :: pipeline_options) :: + @callback handle_init(context :: CallbackContext.t(), options :: pipeline_options) :: callback_return_t() @doc """ @@ -112,7 +112,7 @@ defmodule Membrane.Pipeline do By default it returns `t:Membrane.Pipeline.Action.terminate_t/0` with reason `:normal`. """ - @callback handle_terminate_request(context :: CallbackContext.TerminateRequest.t(), state) :: + @callback handle_terminate_request(context :: CallbackContext.t(), state) :: callback_return_t() @doc """ @@ -121,7 +121,7 @@ defmodule Membrane.Pipeline do Any long-lasting or complex initialization should happen here. """ @callback handle_setup( - context :: CallbackContext.Setup.t(), + context :: CallbackContext.t(), state ) :: callback_return_t @@ -130,7 +130,7 @@ defmodule Membrane.Pipeline do Callback invoked when pipeline switches the playback to `:playing`. """ @callback handle_playing( - context :: CallbackContext.Playing.t(), + context :: CallbackContext.t(), state ) :: callback_return_t @@ -141,7 +141,7 @@ defmodule Membrane.Pipeline do @callback handle_child_notification( notification :: Membrane.ChildNotification.t(), element :: Child.name_t(), - context :: CallbackContext.ChildNotification.t(), + context :: CallbackContext.t(), state ) :: callback_return_t @@ -153,7 +153,7 @@ defmodule Membrane.Pipeline do """ @callback handle_info( message :: any, - context :: CallbackContext.Info.t(), + context :: CallbackContext.t(), state ) :: callback_return_t @@ -164,7 +164,7 @@ defmodule Membrane.Pipeline do @callback handle_element_start_of_stream( child :: Child.name_t(), pad :: Pad.ref_t(), - context :: CallbackContext.StreamManagement.t(), + context :: CallbackContext.t(), state ) :: callback_return_t @@ -174,7 +174,7 @@ defmodule Membrane.Pipeline do @callback handle_element_end_of_stream( child :: Child.name_t(), pad :: Pad.ref_t(), - context :: CallbackContext.StreamManagement.t(), + context :: CallbackContext.t(), state ) :: callback_return_t @@ -183,7 +183,7 @@ defmodule Membrane.Pipeline do """ @callback handle_spec_started( children :: [Child.name_t()], - context :: CallbackContext.SpecStarted.t(), + context :: CallbackContext.t(), state ) :: callback_return_t @@ -193,7 +193,7 @@ defmodule Membrane.Pipeline do """ @callback handle_tick( timer_id :: any, - context :: CallbackContext.Tick.t(), + context :: CallbackContext.t(), state ) :: callback_return_t @@ -202,7 +202,7 @@ defmodule Membrane.Pipeline do """ @callback handle_crash_group_down( group_name :: Child.group_t(), - context :: CallbackContext.CrashGroupDown.t(), + context :: CallbackContext.t(), state ) :: callback_return_t @@ -211,7 +211,7 @@ defmodule Membrane.Pipeline do """ @callback handle_call( message :: any, - context :: CallbackContext.Call.t(), + context :: CallbackContext.t(), state ) :: callback_return_t diff --git a/lib/membrane/pipeline/callback_context.ex b/lib/membrane/pipeline/callback_context.ex new file mode 100644 index 000000000..44bb14984 --- /dev/null +++ b/lib/membrane/pipeline/callback_context.ex @@ -0,0 +1,35 @@ +defmodule Membrane.Pipeline.CallbackContext do + @moduledoc """ + Module describing context passed to the `Membrane.Pipeline` callbacks. + """ + + @type t :: %{ + :clock => Membrane.Clock.t(), + :children => %{Membrane.Child.name_t() => Membrane.ChildEntry.t()}, + :playback => Membrane.Playback.t(), + :resource_guard => Membrane.ResourceGuard.t(), + :utility_supervisor => Membrane.UtilitySupervisor.t(), + optional(:from) => [GenServer.from()], + optional(:members) => [Membrane.Child.name_t()], + optional(:crash_initiator) => Membrane.Child.name_t() + } + + @type option_t :: + {:from, [GenServer.from()]} + | {:members, [Membrane.Child.name_t()]} + | {:crash_initiator, Membrane.Child.name_t()} + + @type options_t :: [option_t()] + + @spec from_state(Membrane.Core.Bin.State.t(), options_t()) :: t() + def from_state(state, additional_fields \\ []) do + Map.new(additional_fields) + |> Map.merge(%{ + clock: state.synchronization.clock_proxy, + children: state.children, + playback: state.playback, + resource_guard: state.resource_guard, + utility_supervisor: state.subprocess_supervisor + }) + end +end diff --git a/lib/membrane/pipeline/callback_context/call.ex b/lib/membrane/pipeline/callback_context/call.ex deleted file mode 100644 index f6190d1f3..000000000 --- a/lib/membrane/pipeline/callback_context/call.ex +++ /dev/null @@ -1,8 +0,0 @@ -defmodule Membrane.Pipeline.CallbackContext.Call do - @moduledoc """ - Structure representing a context that is passed to the callback when the - pipeline is called with a synchronous call. - """ - use Membrane.Core.Pipeline.CallbackContext, - from: [GenServer.from()] -end diff --git a/lib/membrane/pipeline/callback_context/child_notification.ex b/lib/membrane/pipeline/callback_context/child_notification.ex deleted file mode 100644 index f8e7814ee..000000000 --- a/lib/membrane/pipeline/callback_context/child_notification.ex +++ /dev/null @@ -1,7 +0,0 @@ -defmodule Membrane.Pipeline.CallbackContext.ChildNotification do - @moduledoc """ - Structure representing a context that is passed to the callback when - pipeline receives a child notification. - """ - use Membrane.Core.Pipeline.CallbackContext -end diff --git a/lib/membrane/pipeline/callback_context/crash_group_down.ex b/lib/membrane/pipeline/callback_context/crash_group_down.ex deleted file mode 100644 index fde2b6b41..000000000 --- a/lib/membrane/pipeline/callback_context/crash_group_down.ex +++ /dev/null @@ -1,9 +0,0 @@ -defmodule Membrane.Pipeline.CallbackContext.CrashGroupDown do - @moduledoc """ - Structure representing a context that is passed to the bin - when a crash group is down. - """ - use Membrane.Core.Pipeline.CallbackContext, - members: [Membrane.Child.name_t()], - crash_initiator: Membrane.Child.name_t() -end diff --git a/lib/membrane/pipeline/callback_context/info.ex b/lib/membrane/pipeline/callback_context/info.ex deleted file mode 100644 index f86ddbdbf..000000000 --- a/lib/membrane/pipeline/callback_context/info.ex +++ /dev/null @@ -1,7 +0,0 @@ -defmodule Membrane.Pipeline.CallbackContext.Info do - @moduledoc """ - Structure representing a context that is passed to the callback when - pipeline receives an unrecognized message. - """ - use Membrane.Core.Pipeline.CallbackContext -end diff --git a/lib/membrane/pipeline/callback_context/init.ex b/lib/membrane/pipeline/callback_context/init.ex deleted file mode 100644 index 7f9455793..000000000 --- a/lib/membrane/pipeline/callback_context/init.ex +++ /dev/null @@ -1,6 +0,0 @@ -defmodule Membrane.Pipeline.CallbackContext.Init do - @moduledoc """ - Callback context for `c:Membrane.Pipeline.handle_init/2`. - """ - use Membrane.Core.Pipeline.CallbackContext -end diff --git a/lib/membrane/pipeline/callback_context/playing.ex b/lib/membrane/pipeline/callback_context/playing.ex deleted file mode 100644 index e706ea708..000000000 --- a/lib/membrane/pipeline/callback_context/playing.ex +++ /dev/null @@ -1,6 +0,0 @@ -defmodule Membrane.Pipeline.CallbackContext.Playing do - @moduledoc """ - Structure representing a context that is passed to the `c:Membrane.Pipeline.handle_playing/2` callback. - """ - use Membrane.Core.Pipeline.CallbackContext -end diff --git a/lib/membrane/pipeline/callback_context/setup.ex b/lib/membrane/pipeline/callback_context/setup.ex deleted file mode 100644 index ef9316b77..000000000 --- a/lib/membrane/pipeline/callback_context/setup.ex +++ /dev/null @@ -1,6 +0,0 @@ -defmodule Membrane.Pipeline.CallbackContext.Setup do - @moduledoc """ - Structure representing a context that is passed to the `c:Membrane.Pipeline.handle_setup/2` callback. - """ - use Membrane.Core.Pipeline.CallbackContext -end diff --git a/lib/membrane/pipeline/callback_context/spec_started.ex b/lib/membrane/pipeline/callback_context/spec_started.ex deleted file mode 100644 index 31da33c66..000000000 --- a/lib/membrane/pipeline/callback_context/spec_started.ex +++ /dev/null @@ -1,7 +0,0 @@ -defmodule Membrane.Pipeline.CallbackContext.SpecStarted do - @moduledoc """ - Structure representing a context that is passed to the callback of the pipeline - when it instantiates children and links them according to `Membrane.ChildrenSpec` - """ - use Membrane.Core.Pipeline.CallbackContext -end diff --git a/lib/membrane/pipeline/callback_context/stream_management.ex b/lib/membrane/pipeline/callback_context/stream_management.ex deleted file mode 100644 index 41431399b..000000000 --- a/lib/membrane/pipeline/callback_context/stream_management.ex +++ /dev/null @@ -1,7 +0,0 @@ -defmodule Membrane.Pipeline.CallbackContext.StreamManagement do - @moduledoc """ - Structure representing a context that is passed to the pipeline - when handling start and end of stream events. - """ - use Membrane.Core.Pipeline.CallbackContext -end diff --git a/lib/membrane/pipeline/callback_context/terminate_request.ex b/lib/membrane/pipeline/callback_context/terminate_request.ex deleted file mode 100644 index a8f697ecd..000000000 --- a/lib/membrane/pipeline/callback_context/terminate_request.ex +++ /dev/null @@ -1,6 +0,0 @@ -defmodule Membrane.Pipeline.CallbackContext.TerminateRequest do - @moduledoc """ - Callback context for `c:Membrane.Pipeline.handle_terminate_request/2`. - """ - use Membrane.Core.Pipeline.CallbackContext -end diff --git a/lib/membrane/pipeline/callback_context/tick.ex b/lib/membrane/pipeline/callback_context/tick.ex deleted file mode 100644 index b425da2ac..000000000 --- a/lib/membrane/pipeline/callback_context/tick.ex +++ /dev/null @@ -1,7 +0,0 @@ -defmodule Membrane.Pipeline.CallbackContext.Tick do - @moduledoc """ - Structure representing a context that is passed to the callback when - pipeline handles timer tick. - """ - use Membrane.Core.Pipeline.CallbackContext -end diff --git a/lib/membrane/sink.ex b/lib/membrane/sink.ex index c7bfbee7e..e59ec9642 100644 --- a/lib/membrane/sink.ex +++ b/lib/membrane/sink.ex @@ -32,7 +32,7 @@ defmodule Membrane.Sink do @callback handle_write_list( pad :: Pad.ref_t(), buffers :: list(Buffer.t()), - context :: CallbackContext.Write.t(), + context :: CallbackContext.t(), state :: Element.state_t() ) :: Membrane.Element.Base.callback_return_t() @@ -45,7 +45,7 @@ defmodule Membrane.Sink do @callback handle_write( pad :: Pad.ref_t(), buffer :: Buffer.t(), - context :: CallbackContext.Write.t(), + context :: CallbackContext.t(), state :: Element.state_t() ) :: Membrane.Element.Base.callback_return_t() diff --git a/test/membrane/element_test.exs b/test/membrane/element_test.exs index e5b63a339..6036b751c 100644 --- a/test/membrane/element_test.exs +++ b/test/membrane/element_test.exs @@ -99,6 +99,7 @@ defmodule Membrane.ElementTest do end describe "End of stream" do + @tag :target test "causes handle_end_of_stream/3 to be called", %{pipeline: pipeline} do assert_pipeline_play(pipeline) diff --git a/test/membrane/filter_aggregator/unit_test.exs b/test/membrane/filter_aggregator/unit_test.exs index 59d5d3a03..ad06727b3 100644 --- a/test/membrane/filter_aggregator/unit_test.exs +++ b/test/membrane/filter_aggregator/unit_test.exs @@ -7,8 +7,6 @@ defmodule Membrane.FilterAggregator.UnitTest do alias Membrane.Element.PadData alias Membrane.FilterAggregator - alias Membrane.Element.CallbackContext.{Event, Playing, Process, StreamManagement} - alias Membrane.StreamFormat.Mock, as: MockStreamFormat defmodule ElementWithMembranePads do @@ -176,7 +174,7 @@ defmodule Membrane.FilterAggregator.UnitTest do test "handle_playing with stream format sending", test_ctx do expect(FilterA, :handle_playing, fn ctx_a, %{module: FilterA} = state -> - assert %Playing{ + assert %{ clock: nil, name: :a, pads: pads, @@ -218,7 +216,7 @@ defmodule Membrane.FilterAggregator.UnitTest do # ensure proper callbacks order assert state == %{module: FilterB, state: :stream_format_sent} - assert %Playing{ + assert %{ clock: nil, name: :b, pads: pads, @@ -279,11 +277,11 @@ defmodule Membrane.FilterAggregator.UnitTest do buffers_count = Enum.count(test_range) FilterA - |> expect(:handle_process_list, fn :input, buffers, %Process{}, %{module: FilterA} = state -> + |> expect(:handle_process_list, fn :input, buffers, %{}, %{module: FilterA} = state -> args_list = buffers |> Enum.map(&[:input, &1]) {[split: {:handle_process, args_list}], state} end) - |> expect(:handle_process, buffers_count, fn :input, buffer, %Process{}, state -> + |> expect(:handle_process, buffers_count, fn :input, buffer, %{}, state -> assert state.module == FilterA assert %Buffer{payload: <>} = buffer out_payload = payload + 1 @@ -292,7 +290,7 @@ defmodule Membrane.FilterAggregator.UnitTest do end) FilterB - |> expect(:handle_process_list, buffers_count, fn :input, [buffer], %Process{}, state -> + |> expect(:handle_process_list, buffers_count, fn :input, [buffer], %{}, state -> assert state.module == FilterB assert %Buffer{payload: <>} = buffer out_payload = payload * 2 @@ -325,29 +323,29 @@ defmodule Membrane.FilterAggregator.UnitTest do buffer = %Buffer{payload: "test"} FilterA - |> expect(:handle_start_of_stream, fn :input, %StreamManagement{} = ctx, state -> + |> expect(:handle_start_of_stream, fn :input, %{} = ctx, state -> assert ctx.pads.input.start_of_stream? == true {[], state} end) - |> expect(:handle_process_list, fn :input, [^buffer], %Process{} = ctx, state -> + |> expect(:handle_process_list, fn :input, [^buffer], %{} = ctx, state -> assert ctx.pads.input.start_of_stream? == true {[forward: [buffer]], state} end) - |> expect(:handle_end_of_stream, fn :input, %StreamManagement{} = ctx, state -> + |> expect(:handle_end_of_stream, fn :input, %{} = ctx, state -> assert ctx.pads.input.end_of_stream? == true {[forward: :end_of_stream], %{state | state: :ok}} end) FilterB - |> expect(:handle_start_of_stream, fn :input, %StreamManagement{} = ctx, state -> + |> expect(:handle_start_of_stream, fn :input, %{} = ctx, state -> assert ctx.pads.input.start_of_stream? == true {[], state} end) - |> expect(:handle_process_list, fn :input, [^buffer], %Process{} = ctx, state -> + |> expect(:handle_process_list, fn :input, [^buffer], %{} = ctx, state -> assert ctx.pads.input.start_of_stream? == true {[buffer: {:output, [buffer]}], state} end) - |> expect(:handle_end_of_stream, fn :input, %StreamManagement{} = ctx, state -> + |> expect(:handle_end_of_stream, fn :input, %{} = ctx, state -> assert ctx.pads.input.end_of_stream? == true {[end_of_stream: :output], %{state | state: :ok}} end) @@ -379,12 +377,12 @@ defmodule Membrane.FilterAggregator.UnitTest do event = %Discontinuity{} FilterA - |> expect(:handle_event, fn :input, ^event, %Event{}, state -> + |> expect(:handle_event, fn :input, ^event, %{}, state -> {[forward: event], %{state | state: :ok}} end) FilterB - |> expect(:handle_event, fn :input, ^event, %Event{}, state -> + |> expect(:handle_event, fn :input, ^event, %{}, state -> {[event: {:output, event}], %{state | state: :ok}} end) diff --git a/test/support/bin/test_bins.ex b/test/support/bin/test_bins.ex index 448e11a61..37e694c1a 100644 --- a/test/support/bin/test_bins.ex +++ b/test/support/bin/test_bins.ex @@ -1,6 +1,5 @@ defmodule Membrane.Support.Bin.TestBins do @moduledoc false - alias Membrane.ChildrenSpec defmodule TestFilter do @moduledoc false