Skip to content

Commit

Permalink
Set callback contexts to be maps
Browse files Browse the repository at this point in the history
  • Loading branch information
FelonEkonom committed Jan 9, 2023
1 parent 2f3dfc4 commit 5499334
Show file tree
Hide file tree
Showing 75 changed files with 301 additions and 626 deletions.
29 changes: 16 additions & 13 deletions lib/membrane/bin.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand All @@ -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

Expand All @@ -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

Expand All @@ -73,15 +73,15 @@ 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

@doc """
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
Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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,
Expand Down
36 changes: 36 additions & 0 deletions lib/membrane/bin/callback_context.ex
Original file line number Diff line number Diff line change
@@ -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
7 changes: 0 additions & 7 deletions lib/membrane/bin/callback_context/child_notification.ex

This file was deleted.

9 changes: 0 additions & 9 deletions lib/membrane/bin/callback_context/crash_group_down.ex

This file was deleted.

7 changes: 0 additions & 7 deletions lib/membrane/bin/callback_context/info.ex

This file was deleted.

6 changes: 0 additions & 6 deletions lib/membrane/bin/callback_context/init.ex

This file was deleted.

9 changes: 0 additions & 9 deletions lib/membrane/bin/callback_context/pad_added.ex

This file was deleted.

8 changes: 0 additions & 8 deletions lib/membrane/bin/callback_context/pad_removed.ex

This file was deleted.

7 changes: 0 additions & 7 deletions lib/membrane/bin/callback_context/parent_notification.ex

This file was deleted.

6 changes: 0 additions & 6 deletions lib/membrane/bin/callback_context/playing.ex

This file was deleted.

6 changes: 0 additions & 6 deletions lib/membrane/bin/callback_context/setup.ex

This file was deleted.

7 changes: 0 additions & 7 deletions lib/membrane/bin/callback_context/spec_started.ex

This file was deleted.

7 changes: 0 additions & 7 deletions lib/membrane/bin/callback_context/stream_management.ex

This file was deleted.

6 changes: 0 additions & 6 deletions lib/membrane/bin/callback_context/terminate_request.ex

This file was deleted.

7 changes: 0 additions & 7 deletions lib/membrane/bin/callback_context/tick.ex

This file was deleted.

4 changes: 1 addition & 3 deletions lib/membrane/core/bin.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}
)
Expand Down
29 changes: 0 additions & 29 deletions lib/membrane/core/bin/callback_context.ex

This file was deleted.

13 changes: 5 additions & 8 deletions lib/membrane/core/bin/pad_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand All @@ -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
)
Expand Down
Loading

0 comments on commit 5499334

Please sign in to comment.