Skip to content

Commit 00aaab8

Browse files
author
kidp330
committed
Update 10_Sink.md
1 parent 8afb2b7 commit 00aaab8

File tree

1 file changed

+30
-32
lines changed

1 file changed

+30
-32
lines changed

basic_pipeline/10_Sink.md

+30-32
Original file line numberDiff line numberDiff line change
@@ -4,77 +4,75 @@ The sink is the last [element](../glossary/glossary.md#element) in our [pipeline
44
In contrast to the [filter elements](../glossary/glossary.md#filter), it won't have any output [pad](../glossary/glossary.md#pad) - that is why we need to make our element `use Membrane.Sink` and define the input pad only.
55
Since we want to parameterize the usage of that element, it will be good to define the options structure, so that we can specify the path to the file where the output should be saved. This stuff is done in the code snippet below:
66

7-
**_`lib/elements/Sink.ex`_**
7+
**_`lib/elements/sink.ex`_**
88

99
```elixir
10-
1110
defmodule Basic.Elements.Sink do
12-
@moduledoc """
13-
An element writing the data to the text file.
14-
"""
15-
16-
use Membrane.Sink
11+
use Membrane.Sink
1712

18-
def_options(location: [type: :string, description: "Path to the file"])
13+
def_options location: [
14+
spec: String.t(),
15+
description: "Path to the file"
16+
]
1917

20-
def_input_pad(:input, demand_unit: :buffers, caps: :any)
18+
def_input_pad :input,
19+
flow_control: :manual,
20+
demand_unit: :buffers,
21+
accepted_format: _any
2122
...
2223
end
2324
```
2425

2526
No surprises there - now we need to specify the element's behavior by defining the relevant callbacks!
2627

27-
**_`lib/elements/Sink.ex`_**
28+
**_`lib/elements/sink.ex`_**
2829

2930
```elixir
3031

3132
defmodule Basic.Elements.Sink do
3233
...
33-
@impl true
34-
def handle_init(options) do
35-
{:ok,
36-
%{
37-
location: options.location
38-
} }
39-
end
34+
@impl true
35+
def handle_init(_context, options) do
36+
{[], %{location: options.location}}
37+
end
4038
...
4139
end
4240
```
4341

44-
We have started with `handle_init/1`, where we are initializing the state of the element (we need to store the path to the output files).
42+
We have started with `handle_init/2`, where we are initializing the state of the element (we need to store the path to the output files).
4543

46-
Later on, we can specify the `handle_prepared_to_playing/2` callback - this callback gets called once the pipeline gets in the `:playing` state - that is a moment when we can demand the [buffers](../glossary/glossary.md#buffer) for the first time (since the pipeline is already prepared to work):
44+
Later on, we can specify the `handle_playing/2` callback - this callback gets called once the pipeline's setup finishes - that is a moment when we can demand the [buffers](../glossary/glossary.md#buffer) for the first time (since the pipeline is already prepared to work):
4745

48-
**_`lib/elements/Sink.ex`_**
46+
**_`lib/elements/sink.ex`_**
4947

5048
```elixir
5149

5250
defmodule Basic.Elements.Sink do
5351
...
54-
@impl true
55-
def handle_prepared_to_playing(_ctx, state) do
56-
{ {:ok, demand: {:input, 10} }, state}
57-
end
52+
@impl true
53+
def handle_playing(_context, state) do
54+
{[demand: {:input, 10}], state}
55+
end
5856
...
5957
end
6058
```
6159

62-
There is only one more callback that needs to be specified - `handle_write/4`, which get's called once there are some buffers that can be processed (which means, that there are buffers to be written since there are no output pads through which we could be transmitting these buffers):
60+
There is only one more callback that needs to be specified - `handle_buffer/4`, which get's called once there are some buffers that can be processed (which means, that there are buffers to be written since there are no output pads through which we could be transmitting these buffers):
6361

64-
**_`lib/elements/Sink.ex`_**
62+
**_`lib/elements/sink.ex`_**
6563

6664
```elixir
6765

6866
defmodule Basic.Elements.Sink do
6967
...
70-
@impl true
71-
def handle_write(:input, buffer, _ctx, state) do
72-
File.write!(state.location, buffer.payload <> "\n", [:append])
73-
{ {:ok, demand: {:input, 10} }, state}
74-
end
68+
@impl true
69+
def handle_buffer(:input, buffer, _context, state) do
70+
File.write!(state.location, buffer.payload <> "\n", [:append])
71+
{[demand: {:input, 10}], state}
72+
end
7573
end
7674
```
7775

78-
Note, that after the successful writing, we are taking the `:demand` action and we ask for some more buffer.
76+
Note that after the successful writing, we are taking the `:demand` action and we ask for some more buffer.
7977

8078
With the `Sink` completed, we have implemented all elements of our pipeline. Now let's move to the very last step - defining the actual pipeline using the elements we have created.

0 commit comments

Comments
 (0)