You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: basic_pipeline/10_Sink.md
+30-32
Original file line number
Diff line number
Diff line change
@@ -4,77 +4,75 @@ The sink is the last [element](../glossary/glossary.md#element) in our [pipeline
4
4
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.
5
5
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:
6
6
7
-
**_`lib/elements/Sink.ex`_**
7
+
**_`lib/elements/sink.ex`_**
8
8
9
9
```elixir
10
-
11
10
defmoduleBasic.Elements.Sinkdo
12
-
@moduledoc """
13
-
An element writing the data to the text file.
14
-
"""
15
-
16
-
useMembrane.Sink
11
+
useMembrane.Sink
17
12
18
-
def_options(location: [type::string, description:"Path to the file"])
No surprises there - now we need to specify the element's behavior by defining the relevant callbacks!
26
27
27
-
**_`lib/elements/Sink.ex`_**
28
+
**_`lib/elements/sink.ex`_**
28
29
29
30
```elixir
30
31
31
32
defmoduleBasic.Elements.Sinkdo
32
33
...
33
-
@impltrue
34
-
defhandle_init(options) do
35
-
{:ok,
36
-
%{
37
-
location: options.location
38
-
} }
39
-
end
34
+
@impltrue
35
+
defhandle_init(_context, options) do
36
+
{[], %{location: options.location}}
37
+
end
40
38
...
41
39
end
42
40
```
43
41
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).
45
43
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):
47
45
48
-
**_`lib/elements/Sink.ex`_**
46
+
**_`lib/elements/sink.ex`_**
49
47
50
48
```elixir
51
49
52
50
defmoduleBasic.Elements.Sinkdo
53
51
...
54
-
@impltrue
55
-
defhandle_prepared_to_playing(_ctx, state) do
56
-
{ {:ok, demand: {:input, 10} }, state}
57
-
end
52
+
@impltrue
53
+
defhandle_playing(_context, state) do
54
+
{[demand: {:input, 10}], state}
55
+
end
58
56
...
59
57
end
60
58
```
61
59
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):
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.
79
77
80
78
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