-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathpipeline.ex
More file actions
200 lines (166 loc) · 5.52 KB
/
Copy pathpipeline.ex
File metadata and controls
200 lines (166 loc) · 5.52 KB
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
defmodule Membrane.Core.Pipeline do
@moduledoc false
use GenServer
alias __MODULE__.{ActionHandler, State}
alias Membrane.{Clock, ResourceGuard}
alias Membrane.Core.{CallbackHandler, ProcessHelper, Stalker, SubprocessSupervisor}
alias Membrane.Core.Parent.{ChildLifeController, LifecycleController}
alias Membrane.Core.Pipeline.CallbackContext
alias Membrane.Core.TimerController
require Membrane.Core.Utils, as: Utils
require Membrane.Core.Message, as: Message
require Membrane.Core.Component
require Membrane.Core.LegacyTelemetry, as: LegacyTelemetry
@spec get_stalker(pipeline :: pid()) :: Membrane.Core.Stalker.t()
def get_stalker(pipeline) do
case Message.call(pipeline, :get_stalker) do
{:ok, stalker} -> stalker
{:error, _reason} -> raise Membrane.PipelineError, "Pipeline #{inspect(pipeline)} not found"
end
end
@impl GenServer
def init(params) do
Utils.log_on_error do
do_init(params)
end
end
defp do_init(params) do
observability_config = %{
name: params.name,
component_type: :pipeline,
pid: self()
}
%{subprocess_supervisor: subprocess_supervisor} = params
SubprocessSupervisor.set_parent_component(subprocess_supervisor, observability_config)
stalker = Stalker.new(observability_config, subprocess_supervisor)
{:ok, resource_guard} =
SubprocessSupervisor.start_utility(subprocess_supervisor, {ResourceGuard, self()})
LegacyTelemetry.report_init(:pipeline)
ResourceGuard.register(resource_guard, fn ->
LegacyTelemetry.report_terminate(:pipeline)
end)
{:ok, clock_proxy} =
SubprocessSupervisor.start_utility(
params.subprocess_supervisor,
{Clock, proxy: true}
)
state = %State{
module: params.module,
synchronization: %{
clock_proxy: clock_proxy,
clock_provider: %{clock: nil, provider: nil},
timers: %{}
},
subprocess_supervisor: subprocess_supervisor,
resource_guard: resource_guard,
stalker: stalker
}
state =
CallbackHandler.exec_and_handle_callback(
:handle_init,
ActionHandler,
%{context: &CallbackContext.from_state/1},
[],
%{state | internal_state: params.options}
)
{:ok, state, {:continue, :setup}}
end
@impl GenServer
def handle_continue(:setup, state) do
Utils.log_on_error do
state = LifecycleController.handle_setup(state)
{:noreply, state}
end
end
@impl GenServer
def handle_info(msg, state) do
Utils.log_on_error do
do_handle_info(msg, state)
end
end
defp do_handle_info(
Message.new(:stream_management_event, [element_name, pad_ref, event, event_params]),
state
) do
state =
LifecycleController.handle_stream_management_event(
event,
element_name,
pad_ref,
event_params,
state
)
{:noreply, state}
end
defp do_handle_info(Message.new(:child_pad_removed, [child, pad]), state) do
state = ChildLifeController.handle_child_pad_removed(child, pad, state)
{:noreply, state}
end
defp do_handle_info(Message.new(:child_notification, [from, notification]), state) do
state = LifecycleController.handle_child_notification(from, notification, state)
{:noreply, state}
end
defp do_handle_info(Message.new(:timer_tick, timer_id), state) do
state = TimerController.handle_tick(timer_id, state)
{:noreply, state}
end
defp do_handle_info(Message.new(:link_response, [link_id, direction]), state) do
state = ChildLifeController.handle_link_response(link_id, direction, state)
{:noreply, state}
end
defp do_handle_info(Message.new(:initialized, child), state) do
state = ChildLifeController.handle_child_initialized(child, state)
{:noreply, state}
end
defp do_handle_info(Message.new(:child_death, [name, reason]), state) do
case ChildLifeController.handle_child_death(name, reason, state) do
{:stop, reason, _state} -> ProcessHelper.notoelo(reason)
{:continue, state} -> {:noreply, state}
end
end
defp do_handle_info(Message.new(:terminate), state) do
state = LifecycleController.handle_terminate_request(state)
{:noreply, state}
end
defp do_handle_info(Message.new(_type, _args, _opts) = message, _state) do
raise Membrane.PipelineError, "Received invalid message #{inspect(message)}"
end
defp do_handle_info({:membrane_clock_ratio, clock, ratio}, state) do
state = TimerController.handle_clock_update(clock, ratio, state)
{:noreply, state}
end
defp do_handle_info(message, state) do
state = LifecycleController.handle_info(message, state)
{:noreply, state}
end
@impl GenServer
def handle_call(msg, from, state) do
Utils.log_on_error do
do_handle_call(msg, from, state)
end
end
defp do_handle_call(Message.new(:get_stalker), _from, state) do
{:reply, {:ok, state.stalker}, state}
end
defp do_handle_call(Message.new(:get_child_pid, child_name), _from, state) do
reply =
with %State{children: %{^child_name => %{pid: child_pid}}} <- state do
{:ok, child_pid}
else
_other -> {:error, :child_not_found}
end
{:reply, reply, state}
end
defp do_handle_call(message, from, state) do
context = &CallbackContext.from_state(&1, from: from)
state =
CallbackHandler.exec_and_handle_callback(
:handle_call,
Membrane.Core.Pipeline.ActionHandler,
%{context: context},
[message],
state
)
{:noreply, state}
end
end