-
Notifications
You must be signed in to change notification settings - Fork 39
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
More docs for v1.2 #923
base: master
Are you sure you want to change the base?
More docs for v1.2 #923
Changes from all commits
2eeb21e
01e330e
262a31c
3c11ae9
53a8b68
a16e2c4
c1e023b
684fa5d
379711d
7362714
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,51 @@ | ||||||
# Lifecycle of Membrane Components | ||||||
|
||||||
The lifecycle of Membrane Components is closely related to the execution of Membrane callbacks within these components. While there are some differences among the lifecycles of Membrane Pipelines, Bins, and Elements, they share many similarities. Let's explore the component lifecycle and identify differences depending on the type of component being addressed. | ||||||
|
||||||
## Initialization | ||||||
The first callback executed in every Membrane Component is `handle_init/2`. This function is executed synchronously and blocks the parent component, except in the case of a Membrane Pipeline, as it does not have a parent. It is advisable to avoid heavy computations within this function. `handle_init/2` is ideally used for spawning children in a pipeline or bin through the `:spec` action. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would argue that a pipeline HAS a parent in the form of a process that spawns it and AFAIK that parent synchronously waits for the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also think it would be good to describe what does it mean that a function is "executed synchronously" - it might not be obvious that the "blocking" phase happens after returning the |
||||||
|
||||||
## Setup | ||||||
Following `handle_init/2` is `handle_setup/2`, which is executed asynchronously. This is an optimal time to set up resources or perform other intensive operations required for the element to function properly. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
## Linking pads | ||||||
For components with pads having `availability: :on_request`, the corresponding `handle_pad_added/3` callbacks are called between `handle_setup/2` and `handle_playing/2` if they are linked in the same spec where the component was spawned. Linking the pad in a different spec from the one spawning the element may lead to `handle_pad_added/3` being invoked after `handle_playing/2`. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As we spoke recently, I believe that linking happens only after the child setup is completed, see https://github.com/membraneframework/membrane_core/blob/master/lib/membrane/core/lifecycle_controller.ex#L14 and
|
||||||
|
||||||
## Playing | ||||||
Once setup is completed, a component can enter the `:playing` state by invoking the `handle_playing/2` callback. Note that: | ||||||
- Components spawned within the same `:spec` always enter the `:playing` state simultaneously. If the setup process for one component takes longer, the others will wait. | ||||||
- Elements and Bins wait for their parent to enter the `playing` state before executing `handle_playing/2`. | ||||||
- By default, after `handle_setup/2`, a component's setup is considered complete. This behavior can be modified by returning `setup: :incomplete` from `handle_setup/2`. The component must then mark its setup as completed by returning `setup: :complete` from another callback, like `handle_info/3`, to enter `:playing` playback. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this one be a part of the |
||||||
|
||||||
## Processing the data (applies only to Elements) | ||||||
After `handle_playing/2`, Elements are prepared to process data flowing through their pads. | ||||||
|
||||||
### Events | ||||||
Events are one type of item that can be sent via an Element's pads and are managed in `handle_event/4`. Events can travel both upstream and downstream relative to the pad’s direction. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about emphasizing that events are the only "stream elements" that can be sent upstream? |
||||||
|
||||||
### Stream Formats | ||||||
The stream format, which defines the type of data carried by `Membrane.Buffer`s, must be declared before the first data buffer and is managed in `handle_stream_format/4`. | ||||||
|
||||||
### Start of Stream | ||||||
This callback (`handle_start_of_stream`) is activated just before processing the first `Membrane.Buffer` from a specific input pad. | ||||||
|
||||||
### Buffers | ||||||
The core of multimedia processing involves handling `Membrane.Buffer`s, which contain multimedia payload and may also include metadata or timestamps, all managed within the `handle_buffer/4` callback. | ||||||
|
||||||
### Demands | ||||||
If the Element has pads with `flow_control: :manual`, entering `:playing` playback allows it to send demand using `:demand` action or to receive it in `handle_demand/5` callback. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about distinguishing between manual input pads (capable of sending :demand action) and manual output pads (for which |
||||||
|
||||||
## After processing the data | ||||||
When an element determines that it will no longer send buffers from a specific pad, it can return `:end_of_stream` action to that pad. The linked element receives this in `handle_end_of_stream/3`. The parent component (either a Bin or Pipeline) is notified via `handle_element_end_of_stream/4`. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
## Terminating | ||||||
Typically, the last callback executed within a Membrane Component is `handle_terminate_request`. By default, it returns a `terminate: :normal` action, concluding the component's lifespan with the reason `:normal`. This behavior can be modified by overriding the default implementation, but ensure to return a `terminate: reason` elsewhere to avoid termination issues in your Pipeline. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure the phrase "concluding the component's lifespan" is widely recognized :D How about something simpler, just like "terminating the component"? |
||||||
|
||||||
## Callbacks not strictly related to the lifecycle | ||||||
Some callbacks are not confined to specific stages of the Membrane Component lifecycle. | ||||||
|
||||||
### Handling parent or child notification | ||||||
`handle_parent_notification/3` and `handle_child_notification/4` can occur at any point during the component's lifecycle and are tasked with managing notifications from a parent or child component, respectively. | ||||||
|
||||||
### Handling messages from non-Membrane Erlang Processes | ||||||
The `handle_info/3` callback is present in all Membrane Components and `handle_call/3` in Membrane Pipelines. These can be triggered at any time while the component is alive, functioning similarly to their substituted in `GenServer`. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,115 @@ | ||||||
# Timer usage examples | ||||||
Exampls below illustrate how to use `:start_timer`, `:timer_interval` and `:stop_timer` actions on the example of `Membrane.Source`, but the API looks the same for all kinds of the Membrane Components | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
put that through Grammarly to avoid typos |
||||||
|
||||||
### Emit empty buffer every 100 milliseconds | ||||||
```elixir | ||||||
defmodule MySource do | ||||||
use Membrane.Source | ||||||
|
||||||
def_output_pad :output, accepted_format: SomeFormat | ||||||
|
||||||
@impl true | ||||||
def handle_init(_ctx, _opts), do: {[], %{}} | ||||||
|
||||||
@impl true | ||||||
def handle_playing(_ctx, state) do | ||||||
# let's start a timer named :my_timer that will tick every 100 milliseconds | ||||||
|
||||||
actions = [ | ||||||
start_timer: {:my_timer, Membrane.Time.milliseconds(100)} | ||||||
] | ||||||
|
||||||
{actions, state} | ||||||
end | ||||||
|
||||||
@impl true | ||||||
def handle_tick(:my_timer, ctx, state) do | ||||||
# in this callback we handle ticks of :my_timer | ||||||
# we send a stream format if it hasn't been sent yet and a buffer | ||||||
|
||||||
maybe_stream_format = | ||||||
if ctx.pads.output.stream_format == nil, | ||||||
do: [stream_format: %SomeFormat{}], | ||||||
else: [] | ||||||
Comment on lines
+30
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about moving stream format sending to the |
||||||
|
||||||
buffer = [buffer: {:output, %Membrane.Buffer{payload: ""}}] | ||||||
|
||||||
{maybe_stream_format ++ buffer, state} | ||||||
end | ||||||
end | ||||||
``` | ||||||
|
||||||
### Emit empty buffer every 100 millisecond if parent hasn't stopped you | ||||||
The source below accepts following notifications from the parent: | ||||||
- `:pause` - after receiving it the source will pause sending buffers. The paused soure can be resumed again. | ||||||
- `:resume` - resumes sending buffers from the paused source. | ||||||
- `:stop` - the stopped source won't send any buffer again. | ||||||
|
||||||
```elixir | ||||||
defmodule MyComplexSource | ||||||
use Membrane.Source | ||||||
|
||||||
def_output_pad :output, accepted_format: SomeFormat | ||||||
|
||||||
@impl true | ||||||
def handle_init(_ctx, _opts) do | ||||||
# after starting a timer, status will always be either :resumed, :paused | ||||||
# or :pause_on_next_handle_tick | ||||||
{[], %{status: nil}} | ||||||
end | ||||||
|
||||||
@impl true | ||||||
def handle_playing(_ctx, state) do | ||||||
# let's start a timer named :my_timer ... | ||||||
start_timer_action = [ | ||||||
start_timer: {:my_timer, Membrane.Time.milliseconds(100)} | ||||||
] | ||||||
|
||||||
# ... and send a stream format | ||||||
actions = start_timer_action ++ [stream_format: %SomeFormat{}] | ||||||
{actions, %{state | status: :resumed}} | ||||||
end | ||||||
|
||||||
@impl true | ||||||
def handle_parent_notification(notification, ctx, _state) when ctx.playback == :stopped do | ||||||
raise "Cannot handle parent notification: #{inspect(notification)} before handle_playing" | ||||||
end | ||||||
|
||||||
@impl true | ||||||
def handle_parent_notification(notification, _ctx, state) when notification in [:pause, :resume, :stop] do | ||||||
case notification do | ||||||
:pause when state.status == :resumed -> | ||||||
# let's postopne pausing :my_timer to the upcomping handle_tick | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
{[], %{state | status: :pause_on_next_handle_tick}} | ||||||
|
||||||
:resume when state.status == :paused -> | ||||||
# resume :my_timer by returning :timer_interval action | ||||||
actions = [timer_interval: {:my_timer, Membrane.Time.milliseconds(100)}] | ||||||
{actions, %{state | status: :resumed}} | ||||||
|
||||||
:resume when state.status == :pause_on_next_handle_tick -> | ||||||
# case when we receive :pause and :resume notifications without a tick | ||||||
# between them | ||||||
{[], %{state | status: :resumed}} | ||||||
|
||||||
:stop -> | ||||||
# stop :my_timer using :stop_timer action | ||||||
{[stop_timer: :my_timer], %{state | status: :stopped}} | ||||||
end | ||||||
end | ||||||
|
||||||
@impl true | ||||||
def handle_tick(:my_timer, _ctx, state) do | ||||||
case state.status do | ||||||
:resumed -> | ||||||
buffer = %Membrane.Buffer{payload: ""} | ||||||
{[buffer: {:output, buffer}], state} | ||||||
|
||||||
:pause_on_next_handle_tick -> | ||||||
# pause :my_timer using :timer_interval action with interval set to :no_interval | ||||||
actions = [timer_interval: {:my_timer, :no_interval}] | ||||||
{actions, %{state | status: :paused}} | ||||||
end | ||||||
end | ||||||
end | ||||||
``` |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please apply the same changes as in case of |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -223,6 +223,8 @@ defmodule Membrane.Element.Action do | |||||
This action sets the latency for the element. | ||||||
|
||||||
This action is permitted only in callback `c:Membrane.Element.Base.handle_init/2`. | ||||||
|
||||||
The example of usage of this actions is [there](../../../guides/timer.md) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
""" | ||||||
@type latency :: {:latency, latency :: Membrane.Time.non_neg()} | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -31,6 +31,34 @@ defmodule Membrane.Pipeline.Action do | |||||
|
||||||
Children's playback is changed to the current pipeline state. | ||||||
`c:Membrane.Pipeline.handle_spec_started/3` callback is executed once it happens. | ||||||
|
||||||
This is an example of a value that could be passed within `spec` action | ||||||
```elixir | ||||||
child(:file_source, %My.File.Source{path: path}) | ||||||
|> child(:demuxer, My.Demuxer) | ||||||
|> via_out(:video) | ||||||
|> child(:decoder, My.Decoder) | ||||||
|> child(:ai_filter, My.AI.Filter{mode: :picasso_effect) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|> child(:encoder, My.Encoder) | ||||||
|> via_in(:video) | ||||||
|> child(:webrtc_sink, My.WebRTC.Sink) | ||||||
``` | ||||||
along with it's visualisation | ||||||
|
||||||
![](assets/images/spec_without_audio.svg) | ||||||
|
||||||
Returning another spec | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
```elixir | ||||||
get_child(:demuxer) | ||||||
|> via_out(:audio) | ||||||
|> child(:scratch_remover, My.Scratch.Remover) | ||||||
|> via_in(:audio) | ||||||
|> get_child(:webrtc_sink) | ||||||
``` | ||||||
|
||||||
will result in having following children topology: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
![](assets/images/spec_with_audio.svg) | ||||||
""" | ||||||
@type spec :: {:spec, ChildrenSpec.t()} | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,6 +68,8 @@ defmodule Membrane.Mixfile do | |
"guides/upgrading/v1.0.0-rc0.md", | ||
"guides/upgrading/v1.0.0-rc1.md", | ||
"guides/upgrading/v1.0.0.md", | ||
"guides/components_lifecycle.md", | ||
"guides/timer.md", | ||
LICENSE: [title: "License"] | ||
], | ||
formatters: ["html"], | ||
|
@@ -146,7 +148,7 @@ defmodule Membrane.Mixfile do | |
{:bunch, "~> 1.6"}, | ||
{:ratio, "~> 3.0 or ~> 4.0"}, | ||
# Development | ||
{:ex_doc, "~> 0.28", only: :dev, runtime: false}, | ||
{:ex_doc, "0.34.2", only: :dev, runtime: false}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why this version? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will check once again but I searched for a version that generates valid docs |
||
{:makeup_diff, "~> 0.1", only: :dev, runtime: false}, | ||
{:dialyxir, "~> 1.1", only: :dev, runtime: false}, | ||
{:credo, "~> 1.7", only: :dev, runtime: false}, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hate to be the fun destroyer there, but have you considered using Mermaid diagrams? It would definitely be easier to maintain them. I think it should be possible to embed them within ex_doc, as they do it here: https://github.com/elixir-lang/elixir/blob/78f63d08313677a680868685701ae79a2459dcc1/lib/elixir/lib/supervisor.ex#L244
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
more info here https://elixirforum.com/t/using-mermaid-with-ex-doc/40727
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a problem with using Mermaid that I cannot label pads there