/
pipeline.ex
528 lines (447 loc) · 17.4 KB
/
pipeline.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
defmodule Membrane.Pipeline do
@moduledoc """
Module containing functions for constructing and supervising pipelines.
Pipelines are units that make it possible to instantiate, link and manage
elements and bins in convenient way (actually they should always be used inside
a pipeline). Linking pipeline children together enables them to pass data to one
another, and process it in different ways.
To create a pipeline, use the `__using__/1` macro and implement callbacks
of `Membrane.Pipeline` behaviour. For details on instantiating and linking
children, see `Membrane.ParentSpec`.
"""
use Bunch
alias __MODULE__.{Action, CallbackContext}
alias Membrane.{Child, Pad}
alias Membrane.Core.PlaybackHandler
alias Membrane.CrashGroup
require Membrane.Logger
@typedoc """
Defines options that can be passed to `start/3` / `start_link/3` and received
in `c:handle_init/1` callback.
"""
@type pipeline_options_t :: any
@type state_t :: map | struct
@typedoc """
Defines return values from Pipeline callback functions.
## Return values
* `{:ok, state}` - Save process state, with no actions to change the pipeline.
* `{{:ok, [action]}, state}` - Return a list of actions that will be performed within the
pipline. This can be used to start new children, or to send messages to specific children,
for example. Actions are a tuple of `{type, arguments}`, so may be written in the
form a keyword list. See `Membrane.Pipeline.Action` for more info.
* `{{:error, reason}, state}` - Terminates the pipeline with the given reason.
* `{:error, reason}` - raises a `Membrane.CallbackError` with the error tuple.
"""
@type callback_return_t ::
{:ok | {:ok, [Action.t()]} | {:error, any}, state_t}
| {:error, any}
@doc """
Enables to check whether module is membrane pipeline
"""
@callback membrane_pipeline? :: true
@doc """
Callback invoked on initialization of pipeline process. It should parse options
and initialize pipeline's internal state. Internally it is invoked inside
`c:GenServer.init/1` callback.
"""
@callback handle_init(options :: pipeline_options_t) :: callback_return_t()
@doc """
Callback invoked when pipeline is shutting down.
Internally called in `c:GenServer.terminate/2` callback.
Useful for any cleanup required.
"""
@callback handle_shutdown(reason, state :: state_t) :: :ok
when reason: :normal | :shutdown | {:shutdown, any} | term()
@doc """
Callback invoked when pipeline transition from `:stopped` to `:prepared` state has finished,
that is all of its children are prepared to enter `:playing` state.
"""
@callback handle_stopped_to_prepared(
context :: CallbackContext.PlaybackChange.t(),
state :: state_t
) ::
callback_return_t
@doc """
Callback invoked when pipeline transition from `:playing` to `:prepared` state has finished,
that is all of its children are prepared to be stopped.
"""
@callback handle_playing_to_prepared(
context :: CallbackContext.PlaybackChange.t(),
state :: state_t
) ::
callback_return_t
@doc """
Callback invoked when pipeline is in `:playing` state, i.e. all its children
are in this state.
"""
@callback handle_prepared_to_playing(
context :: CallbackContext.PlaybackChange.t(),
state :: state_t
) ::
callback_return_t
@doc """
Callback invoked when pipeline is in `:playing` state, i.e. all its children
are in this state.
"""
@callback handle_prepared_to_stopped(
context :: CallbackContext.PlaybackChange.t(),
state :: state_t
) ::
callback_return_t
@doc """
Callback invoked when pipeline is in `:terminating` state, i.e. all its children
are in this state.
"""
@callback handle_stopped_to_terminating(
context :: CallbackContext.PlaybackChange.t(),
state :: state_t
) :: callback_return_t
@doc """
Callback invoked when a notification comes in from an element.
"""
@callback handle_notification(
notification :: Membrane.Notification.t(),
element :: Child.name_t(),
context :: CallbackContext.Notification.t(),
state :: state_t
) :: callback_return_t
@doc """
Callback invoked when pipeline receives a message that is not recognized
as an internal membrane message.
Useful for receiving data sent from NIFs or other stuff.
"""
@callback handle_other(
message :: any,
context :: CallbackContext.Other.t(),
state :: state_t
) ::
callback_return_t
@doc """
Callback invoked when a child element starts processing stream via given pad.
"""
@callback handle_element_start_of_stream(
{Child.name_t(), Pad.ref_t()},
context :: CallbackContext.StreamManagement.t(),
state :: state_t
) :: callback_return_t
@doc """
Callback invoked when a child element finishes processing stream via given pad.
"""
@callback handle_element_end_of_stream(
{Child.name_t(), Pad.ref_t()},
context :: CallbackContext.StreamManagement.t(),
state :: state_t
) :: callback_return_t
@doc """
Callback invoked when `Membrane.ParentSpec` is linked and in the same playback
state as pipeline.
This callback can be started from `c:handle_init/1` callback or as
`t:Membrane.Pipeline.Action.spec_t/0` action.
"""
@callback handle_spec_started(
children :: [Child.name_t()],
context :: CallbackContext.SpecStarted.t(),
state :: state_t
) :: callback_return_t
@doc """
Callback invoked upon each timer tick. A timer can be started with `Membrane.Pipeline.Action.start_timer_t`
action.
"""
@callback handle_tick(
timer_id :: any,
context :: CallbackContext.Tick.t(),
state :: state_t
) :: callback_return_t
@doc """
Callback invoked when crash of the crash group happens.
"""
@callback handle_crash_group_down(
group_name :: CrashGroup.name_t(),
context :: CallbackContext.CrashGroupDown.t(),
state :: state_t
) :: callback_return_t
@optional_callbacks handle_init: 1,
handle_shutdown: 2,
handle_stopped_to_prepared: 2,
handle_playing_to_prepared: 2,
handle_prepared_to_playing: 2,
handle_prepared_to_stopped: 2,
handle_stopped_to_terminating: 2,
handle_other: 3,
handle_spec_started: 3,
handle_element_start_of_stream: 3,
handle_element_end_of_stream: 3,
handle_notification: 4,
handle_tick: 3,
handle_crash_group_down: 3
@doc """
Starts the Pipeline based on given module and links it to the current
process.
Pipeline options are passed to module's `c:handle_init/1` callback.
Process options are internally passed to `GenServer.start_link/3`.
Returns the same values as `GenServer.start_link/3`.
"""
@spec start_link(
module,
pipeline_options :: pipeline_options_t,
process_options :: GenServer.options()
) :: GenServer.on_start()
def start_link(module, pipeline_options \\ nil, process_options \\ []),
do: do_start(:start_link, module, pipeline_options, process_options)
@doc """
Does the same as `start_link/3` but starts process outside of supervision tree.
"""
@spec start(
module,
pipeline_options :: pipeline_options_t,
process_options :: GenServer.options()
) :: GenServer.on_start()
def start(module, pipeline_options \\ nil, process_options \\ []),
do: do_start(:start, module, pipeline_options, process_options)
defp do_start(method, module, pipeline_options, process_options) do
if module |> pipeline? do
Membrane.Logger.debug("""
Pipeline start link: module: #{inspect(module)},
pipeline options: #{inspect(pipeline_options)},
process options: #{inspect(process_options)}
""")
apply(GenServer, method, [
Membrane.Core.Pipeline,
{module, pipeline_options},
process_options
])
else
Membrane.Logger.error("""
Cannot start pipeline, passed module #{inspect(module)} is not a Membrane Pipeline.
Make sure that given module is the right one and it uses Membrane.Pipeline
""")
{:error, {:not_pipeline, module}}
end
end
@doc """
Changes pipeline's playback state to `:stopped` and terminates its process.
It accpets two options:
* `blocking?` - tells whether to stop the pipeline synchronously
* `timeout` - if `blocking?` is set to true it tells how much
time (ms) to wait for pipeline to get terminated. Defaults to 5000.
"""
@spec stop_and_terminate(pipeline :: pid, Keyword.t()) ::
:ok | {:error, :timeout}
def stop_and_terminate(pipeline, opts \\ []) do
blocking? = Keyword.get(opts, :blocking?, false)
timeout = Keyword.get(opts, :timeout, 5000)
ref = if blocking?, do: Process.monitor(pipeline)
PlaybackHandler.request_playback_state_change(pipeline, :terminating)
if blocking?,
do: wait_for_down(ref, timeout),
else: :ok
end
defp wait_for_down(ref, timeout) do
receive do
{:DOWN, ^ref, _, _, _} ->
:ok
after
timeout ->
{:error, :timeout}
end
end
@doc """
Changes playback state to `:playing`.
An alias for `Membrane.Core.PlaybackHandler.change_playback_state/2` with proper state.
"""
@spec play(pid) :: :ok
def play(pid), do: Membrane.Core.PlaybackHandler.request_playback_state_change(pid, :playing)
@doc """
Changes playback state to `:prepared`.
An alias for `Membrane.Core.PlaybackHandler.change_playback_state/2` with proper state.
"""
@spec prepare(pid) :: :ok
def prepare(pid),
do: Membrane.Core.PlaybackHandler.request_playback_state_change(pid, :prepared)
@doc """
Changes playback state to `:stopped`.
An alias for `Membrane.Core.PlaybackHandler.change_playback_state/2` with proper state.
"""
@spec stop(pid) :: :ok
def stop(pid), do: Membrane.Core.PlaybackHandler.request_playback_state_change(pid, :stopped)
@doc """
Checks whether module is a pipeline.
"""
@spec pipeline?(module) :: boolean
def pipeline?(module) do
module |> Bunch.Module.check_behaviour(:membrane_pipeline?)
end
@doc false
defmacro __before_compile__(_env) do
quote do
unless Enum.any?(0..2, &Module.defines?(__MODULE__, {:start_link, &1})) do
@doc """
Starts the pipeline `#{inspect(__MODULE__)}` and links it to the current process.
A proxy for `#{inspect(unquote(__MODULE__))}.start_link/3`
"""
@spec start_link(
pipeline_options :: unquote(__MODULE__).pipeline_options_t(),
process_options :: GenServer.options()
) :: GenServer.on_start()
def start_link(pipeline_options \\ nil, process_options \\ []) do
unquote(__MODULE__).start_link(__MODULE__, pipeline_options, process_options)
end
end
unless Enum.any?(0..2, &Module.defines?(__MODULE__, {:start, &1})) do
@doc """
Starts the pipeline `#{inspect(__MODULE__)}` without linking it
to the current process.
A proxy for `#{inspect(unquote(__MODULE__))}.start/3`
"""
@spec start(
pipeline_options :: unquote(__MODULE__).pipeline_options_t(),
process_options :: GenServer.options()
) :: GenServer.on_start()
def start(pipeline_options \\ nil, process_options \\ []) do
unquote(__MODULE__).start(__MODULE__, pipeline_options, process_options)
end
end
unless Module.defines?(__MODULE__, {:play, 1}) do
@doc """
Changes playback state of pipeline to `:playing`.
"""
@spec play(pid()) :: :ok
defdelegate play(pipeline), to: unquote(__MODULE__)
end
unless Module.defines?(__MODULE__, {:prepare, 1}) do
@doc """
Changes playback state to `:prepared`.
"""
@spec prepare(pid) :: :ok
defdelegate prepare(pipeline), to: unquote(__MODULE__)
end
unless Module.defines?(__MODULE__, {:stop, 1}) do
@doc """
Changes playback state to `:stopped`.
"""
@spec stop(pid) :: :ok
defdelegate stop(pid), to: unquote(__MODULE__)
end
unless Enum.any?(1..2, &Module.defines?(__MODULE__, {:stop_and_terminate, &1})) do
@doc """
Changes pipeline's playback state to `:stopped` and terminates its process.
"""
@spec stop_and_terminate(pid, Keyword.t()) :: :ok
defdelegate stop_and_terminate(pipeline, opts \\ []), to: unquote(__MODULE__)
end
end
end
@doc """
Brings all the stuff necessary to implement a pipeline.
Options:
- `:bring_spec?` - if true (default) imports and aliases `Membrane.ParentSpec`
- `:bring_pad?` - if true (default) requires and aliases `Membrane.Pad`
"""
defmacro __using__(options) do
bring_spec =
if options |> Keyword.get(:bring_spec?, true) do
quote do
import Membrane.ParentSpec
alias Membrane.ParentSpec
end
end
bring_pad =
if options |> Keyword.get(:bring_pad?, true) do
quote do
require Membrane.Pad
alias Membrane.Pad
end
end
# credo:disable-for-next-line Credo.Check.Refactor.LongQuoteBlocks
quote do
alias unquote(__MODULE__)
@behaviour unquote(__MODULE__)
@before_compile Pipeline
unquote(bring_spec)
unquote(bring_pad)
@impl true
def membrane_pipeline?, do: true
@impl true
def handle_init(_options), do: {{:ok, spec: %Membrane.ParentSpec{}}, %{}}
@impl true
def handle_shutdown(_reason, _state), do: :ok
@impl true
def handle_stopped_to_prepared(_ctx, state), do: handle_stopped_to_prepared(state)
@impl true
def handle_prepared_to_playing(_ctx, state), do: handle_prepared_to_playing(state)
@impl true
def handle_playing_to_prepared(_ctx, state), do: handle_playing_to_prepared(state)
@impl true
def handle_prepared_to_stopped(_ctx, state), do: handle_prepared_to_stopped(state)
@impl true
def handle_stopped_to_terminating(_ctx, state), do: handle_stopped_to_terminating(state)
@impl true
def handle_other(message, _ctx, state), do: handle_other(message, state)
@impl true
def handle_spec_started(new_children, _ctx, state),
do: handle_spec_started(new_children, state)
@impl true
def handle_element_start_of_stream({element, pad}, _ctx, state),
do: handle_element_start_of_stream({element, pad}, state)
@impl true
def handle_element_end_of_stream({element, pad}, _ctx, state),
do: handle_element_end_of_stream({element, pad}, state)
@impl true
def handle_notification(notification, element, _ctx, state),
do: handle_notification(notification, element, state)
@impl true
def handle_crash_group_down(_group_name, _ctx, state) do
{:ok, state}
end
# DEPRECATED:
# credo:disable-for-lines:21 Credo.Check.Readability.Specs
@doc false
def handle_stopped_to_prepared(state), do: {:ok, state}
@doc false
def handle_prepared_to_playing(state), do: {:ok, state}
@doc false
def handle_playing_to_prepared(state), do: {:ok, state}
@doc false
def handle_prepared_to_stopped(state), do: {:ok, state}
@doc false
def handle_stopped_to_terminating(state), do: {:ok, state}
@doc false
def handle_other(_message, state), do: {:ok, state}
@doc false
def handle_spec_started(_new_children, state), do: {:ok, state}
@doc false
def handle_element_start_of_stream({_element, _pad}, state), do: {:ok, state}
@doc false
def handle_element_end_of_stream({_element, _pad}, state), do: {:ok, state}
@doc false
def handle_notification(_notification, _element, state), do: {:ok, state}
deprecated = [
handle_stopped_to_prepared: 1,
handle_prepared_to_playing: 1,
handle_playing_to_prepared: 1,
handle_prepared_to_stopped: 1,
handle_stopped_to_terminating: 1,
handle_other: 2,
handle_spec_started: 2,
handle_element_start_of_stream: 2,
handle_element_end_of_stream: 2,
handle_notification: 3
]
defoverridable [
handle_init: 1,
handle_shutdown: 2,
handle_stopped_to_prepared: 2,
handle_playing_to_prepared: 2,
handle_prepared_to_playing: 2,
handle_prepared_to_stopped: 2,
handle_stopped_to_terminating: 2,
handle_other: 3,
handle_spec_started: 3,
handle_element_start_of_stream: 3,
handle_element_end_of_stream: 3,
handle_notification: 4,
handle_crash_group_down: 3
] ++ deprecated
end
end
end