Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
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 
15 #include "roc_address/interface.h"
16 #include "roc_address/protocol.h"
17 #include "roc_core/attributes.h"
18 #include "roc_core/list.h"
19 #include "roc_core/noncopyable.h"
20 #include "roc_core/shared_ptr.h"
24 #include "roc_netio/network_loop.h"
26 
27 namespace roc {
28 namespace ctl {
29 
30 //! Control loop thread.
31 //! @remarks
32 //! This class is a task-based facade for the whole roc_ctl module.
33 class ControlLoop : public ControlTaskExecutor<ControlLoop>, public core::NonCopyable<> {
34 public:
35  //! Opaque endpoint handle.
36  typedef struct EndpointHandle* EndpointHandle;
37 
38  //! Subclasses for specific tasks.
39  class Tasks {
40  public:
41  //! Create endpoint on given interface.
42  class CreateEndpoint : public ControlTask {
43  public:
44  //! Set task parameters.
46 
47  //! Get handle of the created endpoint.
49 
50  private:
51  friend class ControlLoop;
52 
54  address::Interface iface_;
55  address::Protocol proto_;
56  };
57 
58  //! Delete endpoint, if it exists.
59  class DeleteEndpoint : public ControlTask {
60  public:
61  //! Set task parameters.
63 
64  private:
65  friend class ControlLoop;
66 
67  enum Phase { Phase_Prologue, Phase_Epilogue };
68 
70  Phase phase_;
71  };
72 
73  //! Bind endpoint on local URI.
74  class BindEndpoint : public ControlTask {
75  public:
76  //! Set task parameters.
78 
79  private:
80  friend class ControlLoop;
81 
82  enum Phase { Phase_Prologue, Phase_Epilogue };
83 
85  const address::EndpointUri& uri_;
86  Phase phase_;
87  };
88 
89  //! Connect endpoint on remote URI.
90  class ConnectEndpoint : public ControlTask {
91  public:
92  //! Set task parameters.
94 
95  private:
96  friend class ControlLoop;
97 
98  enum Phase { Phase_Prologue, Phase_Epilogue };
99 
101  const address::EndpointUri& uri_;
102  Phase phase_;
103  };
104 
105  //! Attach sink to endpoint at given URI.
106  class AttachSink : public ControlTask {
107  public:
108  //! Set task parameters.
110  const address::EndpointUri& uri,
111  pipeline::SenderLoop& sink);
112 
113  private:
114  friend class ControlLoop;
115 
117  const address::EndpointUri& uri_;
118  pipeline::SenderLoop& sink_;
119  };
120 
121  //! Detach sink from endpoint.
122  class DetachSink : public ControlTask {
123  public:
124  //! Set task parameters.
126 
127  private:
128  friend class ControlLoop;
129 
131  pipeline::SenderLoop& sink_;
132  };
133 
134  //! Attach source to endpoint at given URI.
135  class AttachSource : public ControlTask {
136  public:
137  //! Set task parameters.
139  const address::EndpointUri& uri,
140  pipeline::ReceiverLoop& source);
141 
142  private:
143  friend class ControlLoop;
144 
146  const address::EndpointUri& uri_;
147  pipeline::ReceiverLoop& source_;
148  };
149 
150  //! Detach source from endpoint.
151  class DetachSource : public ControlTask {
152  public:
153  //! Set task parameters.
155 
156  private:
157  friend class ControlLoop;
158 
160  pipeline::ReceiverLoop& source_;
161  };
162 
163  //! Process pending pipeline tasks on control thread.
165  public:
166  //! Set task parameters.
168 
169  private:
170  friend class ControlLoop;
171 
172  pipeline::PipelineLoop& pipeline_;
173  };
174  };
175 
176  //! Initialize.
178 
179  virtual ~ControlLoop();
180 
181  //! Check if the object was successfully constructed.
182  bool is_valid() const;
183 
184  //! Enqueue a task for asynchronous execution as soon as possible.
185  //! @p completer will be invoked on control thread when the task completes.
186  //! @see ControlTaskQueue::schedule for details.
187  void schedule(ControlTask& task, IControlTaskCompleter* completer);
188 
189  //! Enqueue a task for asynchronous execution at given point of time.
190  //! @p deadline defines the absolute point of time when to execute the task.
191  //! @p completer will be invoked on control thread when the task completes.
192  //! @see ControlTaskQueue::schedule_at for details.
194  core::nanoseconds_t deadline,
195  IControlTaskCompleter* completer);
196 
197  //! Enqueue a task for asynchronous execution and wait until it completes.
198  //! Combines schedule() and wait() calls.
199  //! @returns
200  //! true if the task succeeded or false if it failed.
202 
203  //! Try to cancel scheduled task execution, if it's not executed yet.
204  //! @see ControlTaskQueue::async_cancel for details.
206 
207  //! Wait until the task is completed.
208  //! @see ControlTaskQueue::wait for details.
209  void wait(ControlTask& task);
210 
211 private:
212  ControlTaskResult task_create_endpoint_(ControlTask&);
213  ControlTaskResult task_delete_endpoint_(ControlTask&);
214  ControlTaskResult task_bind_endpoint_(ControlTask&);
215  ControlTaskResult task_connect_endpoint_(ControlTask&);
216  ControlTaskResult task_attach_sink_(ControlTask&);
217  ControlTaskResult task_detach_sink_(ControlTask&);
218  ControlTaskResult task_attach_source_(ControlTask&);
219  ControlTaskResult task_detach_source_(ControlTask&);
220  ControlTaskResult task_pipeline_processing_(ControlTask&);
221 
222  netio::NetworkLoop& network_loop_;
223  core::IArena& arena_;
224 
225  ControlTaskQueue task_queue_;
226 
228 };
229 
230 } // namespace ctl
231 } // namespace roc
232 
233 #endif // ROC_CTL_CONTROL_LOOP_H_
Compiler attributes.
#define ROC_ATTR_NODISCARD
Emit warning if function result is not checked.
Definition: attributes.h:31
Base class for control endpoints.
Network endpoint URI.
Definition: endpoint_uri.h:27
Memory arena interface.
Definition: iarena.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:106
AttachSink(EndpointHandle endpoint, const address::EndpointUri &uri, pipeline::SenderLoop &sink)
Set task parameters.
Attach source to endpoint at given URI.
Definition: control_loop.h:135
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:90
ConnectEndpoint(EndpointHandle endpoint, const address::EndpointUri &uri)
Set task parameters.
Create endpoint on given interface.
Definition: control_loop.h:42
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:59
DeleteEndpoint(EndpointHandle endpoint)
Set task parameters.
DetachSink(EndpointHandle endpoint, pipeline::SenderLoop &sink)
Set task parameters.
Detach source from endpoint.
Definition: control_loop.h:151
DetachSource(EndpointHandle endpoint, pipeline::ReceiverLoop &source)
Set task parameters.
Process pending pipeline tasks on control thread.
Definition: control_loop.h:164
PipelineProcessing(pipeline::PipelineLoop &pipeline)
Set task parameters.
Subclasses for specific tasks.
Definition: control_loop.h:39
Control loop thread.
Definition: control_loop.h:33
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...
ControlLoop(netio::NetworkLoop &network_loop, core::IArena &arena)
Initialize.
ROC_ATTR_NODISCARD bool schedule_and_wait(ControlTask &task)
Enqueue a task for asynchronous execution and wait until it completes. Combines schedule() and wait()...
bool is_valid() const
Check if the object was successfully constructed.
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:36
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:53
Base class for task-based pipelines.
Receiver pipeline loop.
Definition: receiver_loop.h:47
Sender pipeline loop.
Definition: sender_loop.h:44
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.