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

Add docs about default callback implementation #572

Merged
merged 22 commits into from
Jul 3, 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
16 changes: 15 additions & 1 deletion lib/membrane/bin.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ defmodule Membrane.Bin 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, initializing state or
spawning children.
By default, it converts the opts struct to a map and sets them as the bin's state.
"""
@callback handle_init(context :: CallbackContext.t(), options :: options) ::
callback_return()
Expand All @@ -51,6 +52,7 @@ defmodule Membrane.Bin do
ONLY for dynamic pads.

Context passed to this callback contains additional field `:pad_options`.
By default, it does nothing.
"""
@callback handle_pad_added(
pad :: Pad.ref(),
Expand All @@ -63,6 +65,7 @@ defmodule Membrane.Bin do
ONLY for dynamic pads.

Context passed to this callback contains additional field `:pad_options`.
By default, it does nothing.
"""
@callback handle_pad_removed(
pad :: Pad.ref(),
Expand All @@ -74,6 +77,7 @@ defmodule Membrane.Bin do
Callback invoked on bin startup, right after `c:handle_init/2`.

Any long-lasting or complex initialization should happen here.
By default, it does nothing.
"""
@callback handle_setup(
context :: CallbackContext.t(),
Expand All @@ -82,6 +86,7 @@ defmodule Membrane.Bin do

@doc """
Callback invoked when bin switches the playback to `:playing`.
By default, it does nothing.
"""
@callback handle_playing(
context :: CallbackContext.t(),
Expand All @@ -95,6 +100,7 @@ defmodule Membrane.Bin do
The callback won't be invoked, when you have initiated the pad removal,
eg. when you have returned `t:Membrane.Bin.Action.remove_link()` action
which made one of your children's pads be removed.
By default, it does nothing.
"""
@callback handle_child_pad_removed(
child :: Child.name(),
Expand All @@ -105,6 +111,7 @@ defmodule Membrane.Bin do

@doc """
Callback invoked when a notification comes in from an element.
By default, it ignores the received message.
"""
@callback handle_child_notification(
notification :: Membrane.ChildNotification.t(),
Expand All @@ -115,6 +122,7 @@ defmodule Membrane.Bin do

@doc """
Callback invoked when a notification comes in from an parent.
By default, it ignores the received message.
"""
@callback handle_parent_notification(
notification :: Membrane.ParentNotification.t(),
Expand All @@ -127,6 +135,7 @@ defmodule Membrane.Bin do
as an internal membrane message.

Can be used for receiving data from non-membrane processes.
By default, it ignores the received message.
"""
@callback handle_info(
message :: any,
Expand All @@ -136,6 +145,7 @@ defmodule Membrane.Bin do

@doc """
Callback invoked when a child element starts processing stream via given pad.
By default, it does nothing.
"""
@callback handle_element_start_of_stream(
child :: Child.name(),
Expand All @@ -146,6 +156,8 @@ defmodule Membrane.Bin do

@doc """
Callback invoked when a child element finishes processing stream via given pad.

By default, it does nothing.
"""
@callback handle_element_end_of_stream(
child :: Child.name(),
Expand All @@ -156,6 +168,8 @@ defmodule Membrane.Bin do

@doc """
Callback invoked when children of `Membrane.ChildrenSpec` are started.

By default, it does nothing.
Rados13 marked this conversation as resolved.
Show resolved Hide resolved
"""
@callback handle_spec_started(
children :: [Child.name()],
Expand Down Expand Up @@ -187,7 +201,7 @@ defmodule Membrane.Bin do
@doc """
A callback invoked when the bin is being removed by its parent.

By default it returns `t:Membrane.Bin.Action.terminate/0` with reason `:normal`.
By default, it returns `t:Membrane.Bin.Action.terminate/0` with reason `:normal`.
"""
@callback handle_terminate_request(
context :: CallbackContext.t(),
Expand Down
10 changes: 9 additions & 1 deletion lib/membrane/element/base.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ defmodule Membrane.Element.Base do
that happen in this callback crash the parent as well, regardless of crash groups.
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.
By default, it converts the `opts` struct to a map and sets them as the element's state.
"""
@callback handle_init(context :: CallbackContext.t(), options :: Element.options()) ::
callback_return
Expand All @@ -53,6 +54,7 @@ defmodule Membrane.Element.Base do
Callback invoked on element startup, right after `c:handle_init/2`.

Any long-lasting or complex initialization should happen here.
By default, it does nothing.
"""
@callback handle_setup(
context :: CallbackContext.t(),
Expand All @@ -64,6 +66,7 @@ defmodule Membrane.Element.Base do

From this point, element can send and receive buffers, events, stream formats and demands
through its pads.
By default, it does nothing.
"""
@callback handle_playing(
context :: CallbackContext.t(),
Expand All @@ -75,6 +78,7 @@ defmodule Membrane.Element.Base do
as an internal membrane message.

Useful for receiving ticks from timer, data sent from NIFs or other stuff.
By default, it ignores the received message.
"""
@callback handle_info(
message :: any(),
Expand All @@ -87,6 +91,7 @@ defmodule Membrane.Element.Base do
ONLY for dynamic pads.

Context passed to this callback contains additional field `:pad_options`.
By default, it does nothing.
"""
@callback handle_pad_added(
pad :: Pad.ref(),
Expand All @@ -99,6 +104,7 @@ defmodule Membrane.Element.Base do
ONLY for dynamic pads.

Context passed to this callback contains additional field `:pad_options`.
By default, it does nothing.
"""
@callback handle_pad_removed(
pad :: Pad.ref(),
Expand All @@ -111,6 +117,7 @@ defmodule Membrane.Element.Base do

Events may arrive from both input and output pads. In filters by default event is
forwarded to all output and input pads, respectively.
By default, it ignores received event.
"""
@callback handle_event(
pad :: Pad.ref(),
Expand All @@ -131,6 +138,7 @@ defmodule Membrane.Element.Base do

@doc """
Callback invoked when a message from the parent is received.
By default, it ignores the received message.
"""
@callback handle_parent_notification(
notification :: Membrane.ParentNotification.t(),
Expand All @@ -141,7 +149,7 @@ defmodule Membrane.Element.Base do
@doc """
Callback invoked when element is removed by its parent.

By default it returns `t:Membrane.Element.Action.terminate/0` with reason `:normal`.
By default, it returns `t:Membrane.Element.Action.terminate/0` with reason `:normal`.
"""
@callback handle_terminate_request(
context :: CallbackContext.t(),
Expand Down
19 changes: 17 additions & 2 deletions lib/membrane/pipeline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,15 @@ defmodule Membrane.Pipeline do
finishes. For that reason, 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, initializing state or spawning
children.
By default, it converts the `opts` to a map if they're a struct and sets them as the pipeline state.
"""
@callback handle_init(context :: CallbackContext.t(), options :: pipeline_options) ::
callback_return()

@doc """
Callback invoked when pipeline is requested to terminate with `terminate/2`.

By default it returns `t:Membrane.Pipeline.Action.terminate/0` with reason `:normal`.
By default, it returns `t:Membrane.Pipeline.Action.terminate/0` with reason `:normal`.
"""
@callback handle_terminate_request(context :: CallbackContext.t(), state) ::
callback_return()
Expand All @@ -119,6 +120,7 @@ defmodule Membrane.Pipeline do
Callback invoked on pipeline startup, right after `c:handle_init/2`.

Any long-lasting or complex initialization should happen here.
By default, it does nothing.
"""
@callback handle_setup(
context :: CallbackContext.t(),
Expand All @@ -128,6 +130,7 @@ defmodule Membrane.Pipeline do

@doc """
Callback invoked when pipeline switches the playback to `:playing`.
By default, it does nothing.
"""
@callback handle_playing(
context :: CallbackContext.t(),
Expand All @@ -141,6 +144,7 @@ defmodule Membrane.Pipeline do
The callback won't be invoked, when you have initiated the pad removal,
eg. when you have returned `t:Membrane.Pipeline.Action.remove_link()`
action which made one of your children's pads be removed.
By default, it does nothing.
"""
@callback handle_child_pad_removed(
child :: Child.name(),
Expand All @@ -150,7 +154,9 @@ defmodule Membrane.Pipeline do
) :: callback_return

@doc """
Callback invoked when a notification comes in from an element.
Callback invoked when a notification comes in from a child.

By default, it ignores the notification.
"""
@callback handle_child_notification(
notification :: Membrane.ChildNotification.t(),
Expand All @@ -164,6 +170,7 @@ defmodule Membrane.Pipeline do
as an internal membrane message.

Useful for receiving data sent from NIFs or other stuff.
By default, it ignores the received message.
"""
@callback handle_info(
message :: any,
Expand All @@ -174,6 +181,8 @@ defmodule Membrane.Pipeline do

@doc """
Callback invoked when a child element starts processing stream via given pad.

By default, it does nothing.
"""
@callback handle_element_start_of_stream(
child :: Child.name(),
Expand All @@ -184,6 +193,8 @@ defmodule Membrane.Pipeline do

@doc """
Callback invoked when a child element finishes processing stream via given pad.

By default, it does nothing.
"""
@callback handle_element_end_of_stream(
child :: Child.name(),
Expand All @@ -194,6 +205,8 @@ defmodule Membrane.Pipeline do

@doc """
Callback invoked when children of `Membrane.ChildrenSpec` are started.

By default, it does nothing.
Rados13 marked this conversation as resolved.
Show resolved Hide resolved
"""
@callback handle_spec_started(
children :: [Child.name()],
Expand All @@ -215,6 +228,7 @@ defmodule Membrane.Pipeline do
Callback invoked when crash of the crash group happens.

Context passed to this callback contains 2 additional fields: `:members` and `:crash_initiator`.
By default, it does nothing.
"""
@callback handle_crash_group_down(
group_name :: Child.group(),
Expand All @@ -226,6 +240,7 @@ defmodule Membrane.Pipeline do
Callback invoked when pipeline is called using a synchronous call.

Context passed to this callback contains additional field `:from`.
By default, it does nothing.
"""
@callback handle_call(
message :: any,
Expand Down