Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
Loading...
Searching...
No Matches
control_loop.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Roc Streaming authors
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 */
8
9//! @file roc_ctl/control_loop.h
10//! @brief Control loop thread.
11
12#ifndef ROC_CTL_CONTROL_LOOP_H_
13#define ROC_CTL_CONTROL_LOOP_H_
14
17#include "roc_core/list.h"
19#include "roc_core/shared_ptr.h"
25
26namespace roc {
27namespace ctl {
28
29//! Control loop thread.
30//! @remarks
31//! This class is a task-based facade for the whole roc_ctl module.
32class ControlLoop : public ControlTaskExecutor<ControlLoop>, public core::NonCopyable<> {
33public:
34 //! Opaque endpoint handle.
36
37 //! Subclasses for specific tasks.
38 class Tasks {
39 public:
40 //! Create endpoint on given interface.
41 class CreateEndpoint : public ControlTask {
42 public:
43 //! Set task parameters.
45
46 //! Get handle of the created endpoint.
48
49 private:
50 friend class ControlLoop;
51
53 address::Interface iface_;
54 address::Protocol proto_;
55 };
56
57 //! Delete endpoint, if it exists.
58 class DeleteEndpoint : public ControlTask {
59 public:
60 //! Set task parameters.
62
63 private:
64 friend class ControlLoop;
65
66 enum Phase { Phase_Prologue, Phase_Epilogue };
67
69 Phase phase_;
70 };
71
72 //! Bind endpoint on local URI.
73 class BindEndpoint : public ControlTask {
74 public:
75 //! Set task parameters.
77
78 private:
79 friend class ControlLoop;
80
81 enum Phase { Phase_Prologue, Phase_Epilogue };
82
84 const address::EndpointUri& uri_;
85 Phase phase_;
86 };
87
88 //! Connect endpoint on remote URI.
90 public:
91 //! Set task parameters.
93
94 private:
95 friend class ControlLoop;
96
97 enum Phase { Phase_Prologue, Phase_Epilogue };
98
100 const address::EndpointUri& uri_;
101 Phase phase_;
102 };
103
104 //! Attach sink to endpoint at given URI.
105 class AttachSink : public ControlTask {
106 public:
107 //! Set task parameters.
109 const address::EndpointUri& uri,
111
112 private:
113 friend class ControlLoop;
114
116 const address::EndpointUri& uri_;
118 };
119
120 //! Detach sink from endpoint.
121 class DetachSink : public ControlTask {
122 public:
123 //! Set task parameters.
125
126 private:
127 friend class ControlLoop;
128
131 };
132
133 //! Attach source to endpoint at given URI.
134 class AttachSource : public ControlTask {
135 public:
136 //! Set task parameters.
138 const address::EndpointUri& uri,
139 pipeline::ReceiverLoop& source);
140
141 private:
142 friend class ControlLoop;
143
145 const address::EndpointUri& uri_;
146 pipeline::ReceiverLoop& source_;
147 };
148
149 //! Detach source from endpoint.
150 class DetachSource : public ControlTask {
151 public:
152 //! Set task parameters.
154
155 private:
156 friend class ControlLoop;
157
159 pipeline::ReceiverLoop& source_;
160 };
161
162 //! Process pending pipeline tasks on control thread.
164 public:
165 //! Set task parameters.
167
168 private:
169 friend class ControlLoop;
170
171 pipeline::PipelineLoop& pipeline_;
172 };
173 };
174
175 //! Initialize.
177
178 virtual ~ControlLoop();
179
180 //! Check if the object was successfully constructed.
181 bool valid() const;
182
183 //! Enqueue a task for asynchronous execution as soon as possible.
184 //! @p completer will be invoked on control thread when the task completes.
185 //! @see ControlTaskQueue::schedule for details.
187
188 //! Enqueue a task for asynchronous execution at given point of time.
189 //! @p deadline defines the absolute point of time when to execute the task.
190 //! @p completer will be invoked on control thread when the task completes.
191 //! @see ControlTaskQueue::schedule_at for details.
193 core::nanoseconds_t deadline,
194 IControlTaskCompleter* completer);
195
196 //! Enqueue a task for asynchronous execution and wait until it completes.
197 //! Combines schedule() and wait() calls.
198 //! @returns
199 //! true if the task succeeded or false if it failed.
201
202 //! Try to cancel scheduled task execution, if it's not executed yet.
203 //! @see ControlTaskQueue::async_cancel for details.
205
206 //! Wait until the task is completed.
207 //! @see ControlTaskQueue::wait for details.
208 void wait(ControlTask& task);
209
210private:
211 ControlTaskResult task_create_endpoint_(ControlTask&);
212 ControlTaskResult task_delete_endpoint_(ControlTask&);
213 ControlTaskResult task_bind_endpoint_(ControlTask&);
214 ControlTaskResult task_connect_endpoint_(ControlTask&);
215 ControlTaskResult task_attach_sink_(ControlTask&);
216 ControlTaskResult task_detach_sink_(ControlTask&);
217 ControlTaskResult task_attach_source_(ControlTask&);
218 ControlTaskResult task_detach_source_(ControlTask&);
219 ControlTaskResult task_pipeline_processing_(ControlTask&);
220
221 netio::NetworkLoop& network_loop_;
222 core::IAllocator& allocator_;
223
224 ControlTaskQueue task_queue_;
225
227};
228
229} // namespace ctl
230} // namespace roc
231
232#endif // ROC_CTL_CONTROL_LOOP_H_
Base class for control endpoints.
Network endpoint URI.
Definition: endpoint_uri.h:26
Memory allocator interface.
Definition: iallocator.h:23
Intrusive doubly-linked list.
Definition: list.h:35
Base class for non-copyable objects.
Definition: noncopyable.h:23
Shared ownership intrusive pointer.
Definition: shared_ptr.h:32
Attach sink to endpoint at given URI.
Definition: control_loop.h:105
AttachSink(EndpointHandle endpoint, const address::EndpointUri &uri, pipeline::SenderLoop &sink)
Set task parameters.
Attach source to endpoint at given URI.
Definition: control_loop.h:134
AttachSource(EndpointHandle endpoint, const address::EndpointUri &uri, pipeline::ReceiverLoop &source)
Set task parameters.
BindEndpoint(EndpointHandle endpoint, const address::EndpointUri &uri)
Set task parameters.
Connect endpoint on remote URI.
Definition: control_loop.h:89
ConnectEndpoint(EndpointHandle endpoint, const address::EndpointUri &uri)
Set task parameters.
Create endpoint on given interface.
Definition: control_loop.h:41
CreateEndpoint(address::Interface iface, address::Protocol proto)
Set task parameters.
EndpointHandle get_handle() const
Get handle of the created endpoint.
Delete endpoint, if it exists.
Definition: control_loop.h:58
DeleteEndpoint(EndpointHandle endpoint)
Set task parameters.
DetachSink(EndpointHandle endpoint, pipeline::SenderLoop &sink)
Set task parameters.
Detach source from endpoint.
Definition: control_loop.h:150
DetachSource(EndpointHandle endpoint, pipeline::ReceiverLoop &source)
Set task parameters.
Process pending pipeline tasks on control thread.
Definition: control_loop.h:163
PipelineProcessing(pipeline::PipelineLoop &pipeline)
Set task parameters.
Subclasses for specific tasks.
Definition: control_loop.h:38
Control loop thread.
Definition: control_loop.h:32
ControlLoop(netio::NetworkLoop &network_loop, core::IAllocator &allocator)
Initialize.
void schedule_at(ControlTask &task, core::nanoseconds_t deadline, IControlTaskCompleter *completer)
Enqueue a task for asynchronous execution at given point of time. deadline defines the absolute point...
bool valid() const
Check if the object was successfully constructed.
bool schedule_and_wait(ControlTask &task)
Enqueue a task for asynchronous execution and wait until it completes. Combines schedule() and wait()...
void wait(ControlTask &task)
Wait until the task is completed.
void async_cancel(ControlTask &task)
Try to cancel scheduled task execution, if it's not executed yet.
struct EndpointHandle * EndpointHandle
Opaque endpoint handle.
Definition: control_loop.h:35
void schedule(ControlTask &task, IControlTaskCompleter *completer)
Enqueue a task for asynchronous execution as soon as possible. completer will be invoked on control t...
Base class for control tasks.
Definition: control_task.h:53
Control task completion handler.
Network event loop thread.
Definition: network_loop.h:52
Base class for task-based pipelines.
Receiver pipeline loop.
Definition: receiver_loop.h:46
Sender pipeline loop.
Definition: sender_loop.h:43
Control task executor.
Control task queue.
Interface ID.
Intrusive doubly-linked list.
Interface
Interface ID.
Definition: interface.h:19
Protocol
Protocol ID.
Definition: protocol.h:19
int64_t nanoseconds_t
Nanoseconds.
Definition: time.h:58
ControlTaskResult
Control task execution result.
Definition: control_task.h:34
Root namespace.
Network event loop thread.
Non-copyable object.
Base class for pipelines.
Protocol ID.
Shared ownership intrusive pointer.