View Source Membrane.Element.Base behaviour (Membrane Core v0.11.3)

Module defining behaviour common to all elements.

When used declares behaviour implementation, provides default callback definitions and imports macros.

Elements

Elements are units that produce, process or consume data. They can be linked with Membrane.Pipeline, and thus form a pipeline able to perform complex data processing. Each element defines a set of pads, through which it can be linked with other elements. During playback, pads can either send (output pads) or receive (input pads) data. For more information on pads, see Membrane.Pad.

To implement an element, one of base modules (Membrane.Source, Membrane.Filter, Membrane.Endpoint or Membrane.Sink) has to be used, depending on the element type:

  • source, producing buffers (contain only output pads),
  • filter, processing buffers (contain both input and output pads),
  • endpoint, producing and consuming buffers (contain both input and output pads),
  • sink, consuming buffers (contain only input pads). For more information on each element type, check documentation for appropriate base module.

behaviours

Behaviours

Element-specific behaviours are specified in modules:

callbacks

Callbacks

Modules listed above provide specifications of callbacks that define elements lifecycle. All of these callbacks have names with the handle_ prefix. They are used to define reaction to certain events that happen during runtime, and indicate what actions framework should undertake as a result, besides executing element-specific code.

For actions that can be returned by each callback, see Membrane.Element.Action module.

Link to this section Summary

Types

Type that defines all valid return values from most callbacks.

Callbacks

A callback for constructing struct. Will be defined by def_options/1 if used.

A callback for constructing struct with values. Will be defined by def_options/1 if used.

Callback that is called when event arrives.

Callback invoked when element receives a message that is not recognized as an internal membrane message.

Callback invoked on initialization of element.

Callback that is called when new pad has beed added to element. Executed ONLY for dynamic pads.

Callback that is called when some pad of the element has beed removed. Executed ONLY for dynamic pads.

Callback invoked when a message from the parent is received.

Callback invoked when bin switches the playback to :playing.

Callback invoked on element startup, right after handle_init/2.

Callback invoked when element is removed by its parent.

Callback invoked upon each timer tick. A timer can be started with Membrane.Element.Action.start_timer_t action.

Functions

Defines that element exports a clock to pipeline.

Macro defining options that parametrize element.

Link to this section Types

@type callback_return_t() ::
  {[Membrane.Element.Action.t()], Membrane.Element.state_t()}

Type that defines all valid return values from most callbacks.

Link to this section Callbacks

@callback __struct__() :: struct()

A callback for constructing struct. Will be defined by def_options/1 if used.

See defstruct/1 for a more in-depth description.

Link to this callback

__struct__(kv)

View Source (optional)
@callback __struct__(kv :: [atom() | {atom(), any()}]) :: struct()

A callback for constructing struct with values. Will be defined by def_options/1 if used.

See defstruct/1 for a more in-depth description.

Link to this callback

handle_event(pad, event, context, state)

View Source (optional)
@callback handle_event(
  pad :: Membrane.Pad.ref_t(),
  event :: Membrane.Event.t(),
  context :: Membrane.Element.CallbackContext.Event.t(),
  state :: Membrane.Element.state_t()
) :: callback_return_t()

Callback that is called when event arrives.

Events may arrive from both input and output pads. In filters by default event is forwarded to all output and input pads, respectively.

Link to this callback

handle_info(message, context, state)

View Source (optional)
@callback handle_info(
  message :: any(),
  context :: Membrane.Element.CallbackContext.Info.t(),
  state :: Membrane.Element.state_t()
) :: callback_return_t()

Callback invoked when element receives a message that is not recognized as an internal membrane message.

Useful for receiving ticks from timer, data sent from NIFs or other stuff.

Link to this callback

handle_init(context, options)

View Source (optional)
@callback handle_init(
  context :: Membrane.Element.CallbackContext.Init.t(),
  options :: Membrane.Element.options_t()
) :: callback_return_t()

Callback invoked on initialization of element.

This callback is synchronous: the parent waits until it finishes. Also, any failures 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 handle_setup/2, while handle_init should be used for things like parsing options or initializing state.

Link to this callback

handle_pad_added(pad, context, state)

View Source (optional)
@callback handle_pad_added(
  pad :: Membrane.Pad.ref_t(),
  context :: Membrane.Element.CallbackContext.PadAdded.t(),
  state :: Membrane.Element.state_t()
) :: callback_return_t()

Callback that is called when new pad has beed added to element. Executed ONLY for dynamic pads.

Link to this callback

handle_pad_removed(pad, context, state)

View Source (optional)
@callback handle_pad_removed(
  pad :: Membrane.Pad.ref_t(),
  context :: Membrane.Element.CallbackContext.PadRemoved.t(),
  state :: Membrane.Element.state_t()
) :: callback_return_t()

Callback that is called when some pad of the element has beed removed. Executed ONLY for dynamic pads.

Link to this callback

handle_parent_notification(notification, context, state)

View Source (optional)
@callback handle_parent_notification(
  notification :: Membrane.ParentNotification.t(),
  context :: Membrane.Element.CallbackContext.ParentNotification.t(),
  state :: Membrane.Element.state_t()
) :: callback_return_t()

Callback invoked when a message from the parent is received.

Link to this callback

handle_playing(context, state)

View Source (optional)
@callback handle_playing(
  context :: Membrane.Element.CallbackContext.Playing.t(),
  state :: Membrane.Element.state_t()
) :: callback_return_t()

Callback invoked when bin switches the playback to :playing.

From this point, element can send and receive buffers, events, stream formats and demands through its pads.

Link to this callback

handle_setup(context, state)

View Source (optional)
@callback handle_setup(
  context :: Membrane.Element.CallbackContext.Setup.t(),
  state :: Membrane.Element.state_t()
) :: callback_return_t()

Callback invoked on element startup, right after handle_init/2.

Any long-lasting or complex initialization should happen here.

Link to this callback

handle_terminate_request(context, state)

View Source
@callback handle_terminate_request(
  context :: Membrane.Element.CallbackContext.TerminateRequest.t(),
  state :: Membrane.Element.state_t()
) :: callback_return_t()

Callback invoked when element is removed by its parent.

By default it returns Membrane.Element.Action.terminate_t/0 with reason :normal.

Link to this callback

handle_tick(timer_id, context, state)

View Source (optional)
@callback handle_tick(
  timer_id :: any(),
  context :: Membrane.Element.CallbackContext.Tick.t(),
  state :: Membrane.Element.state_t()
) :: callback_return_t()

Callback invoked upon each timer tick. A timer can be started with Membrane.Element.Action.start_timer_t action.

Link to this section Functions

Link to this macro

__using__(options)

View Source (macro)

Brings common stuff needed to implement an element. Used by Membrane.Source.__using__/1, Membrane.Filter.__using__/1, Membrane.Endpoint.__using__/1 and Membrane.Sink.__using__/1.

Options:

  • :bring_pad? - if true (default) requires and aliases Membrane.Pad
Link to this macro

def_clock(doc \\ "")

View Source (macro)

Defines that element exports a clock to pipeline.

Exporting clock allows pipeline to choose it as the pipeline clock, enabling other elements to synchronize with it. Element's clock is accessible via clock field, while pipeline's one - via parent_clock field in callback contexts. Both of them can be used for starting timers.

Link to this macro

def_options(options)

View Source (macro)

Macro defining options that parametrize element.

It automatically generates appropriate struct and documentation.

Options are defined by a keyword list, where each key is an option name and is described by another keyword list with following fields:

  • spec: typespec for value in struct
  • default: default value for option. If not present, value for this option will have to be provided each time options struct is created
  • inspector: function converting fields' value to a string. Used when creating documentation instead of inspect/1, eg. inspector: &Membrane.Time.inspect/1
  • description: string describing an option. It will be used for generating the docs