Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
receiver_loop.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 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_pipeline/receiver_loop.h
10 //! @brief Receiver pipeline loop.
11 
12 #ifndef ROC_PIPELINE_RECEIVER_LOOP_H_
13 #define ROC_PIPELINE_RECEIVER_LOOP_H_
14 
15 #include "roc_core/iarena.h"
16 #include "roc_core/ipool.h"
17 #include "roc_core/mutex.h"
18 #include "roc_core/optional.h"
19 #include "roc_core/stddefs.h"
20 #include "roc_pipeline/config.h"
21 #include "roc_pipeline/metrics.h"
24 #include "roc_sndio/isource.h"
25 
26 namespace roc {
27 namespace pipeline {
28 
29 //! Receiver pipeline loop.
30 //!
31 //! This class acts as a task-based facade for the receiver pipeline subsystem
32 //! of roc_pipeline module (ReceiverSource, ReceiverSlot, ReceiverEndpoint,
33 //! ReceiverSessionGroup, ReceiverSession).
34 //!
35 //! It provides two interfaces:
36 //!
37 //! - sndio::ISource - can be used to retrieve samples from the pipeline
38 //! (should be used from sndio thread)
39 //!
40 //! - PipelineLoop - can be used to schedule tasks on the pipeline
41 //! (can be used from any thread)
42 //!
43 //! @note
44 //! Private inheritance from ISource is used to decorate actual implementation
45 //! of ISource - ReceiverSource, in order to integrate it with PipelineLoop.
46 class ReceiverLoop : public PipelineLoop, private sndio::ISource {
47 public:
48  //! Opaque slot handle.
49  typedef struct SlotHandle* SlotHandle;
50 
51  //! Base task class.
52  class Task : public PipelineTask {
53  protected:
54  friend class ReceiverLoop;
55 
56  Task();
57 
58  bool (ReceiverLoop::*func_)(Task&); //!< Task implementation method.
59 
60  ReceiverSlot* slot_; //!< Slot.
61  ReceiverSlotConfig slot_config_; //!< Slot config.
62  address::Interface iface_; //!< Interface.
63  address::Protocol proto_; //!< Protocol.
64  address::SocketAddr inbound_address_; //!< Inbound packet address.
65  packet::IWriter* inbound_writer_; //!< Inbound packet writer.
66  packet::IWriter* outbound_writer_; //!< Outbound packet writer.
67  ReceiverSlotMetrics* slot_metrics_; //!< Output slot metrics.
68  ReceiverParticipantMetrics* party_metrics_; //!< Output participant metrics.
69  size_t* party_count_; //!< Input/output participant count.
70  };
71 
72  //! Subclasses for specific tasks.
73  class Tasks {
74  public:
75  //! Create new slot.
76  class CreateSlot : public Task {
77  public:
78  //! Set task parameters.
79  CreateSlot(const ReceiverSlotConfig& slot_config);
80 
81  //! Get created slot handle.
83  };
84 
85  //! Delete existing slot.
86  class DeleteSlot : public Task {
87  public:
88  //! Set task parameters.
90  };
91 
92  //! Query slot metrics.
93  class QuerySlot : public Task {
94  public:
95  //! Set task parameters.
96  //! @remarks
97  //! Metrics are written to provided structs.
99  ReceiverSlotMetrics& slot_metrics,
100  ReceiverParticipantMetrics* party_metrics,
101  size_t* party_count);
102  };
103 
104  //! Create endpoint on given interface of the slot.
105  class AddEndpoint : public Task {
106  public:
107  //! Set task parameters.
108  //! @remarks
109  //! Each slot can have one source and zero or one repair endpoint.
110  //! The protocols of endpoints in one slot should be compatible.
112  address::Interface iface,
113  address::Protocol proto,
114  const address::SocketAddr& inbound_address,
115  packet::IWriter* outbound_writer);
116 
117  //! Get packet writer for inbound packets for the endpoint.
118  //! @remarks
119  //! The returned writer may be used from any thread.
121  };
122  };
123 
124  //! Initialize.
126  const ReceiverSourceConfig& source_config,
127  const rtp::EncodingMap& encoding_map,
128  core::IPool& packet_pool,
129  core::IPool& packet_buffer_pool,
130  core::IPool& frame_buffer_pool,
131  core::IArena& arena);
132 
133  //! Check if the pipeline was successfully constructed.
134  bool is_valid() const;
135 
136  //! Get receiver sources.
137  //! @remarks
138  //! Samples received from remote peers become available in this source.
140 
141 private:
142  // Methods of sndio::ISource
143  virtual sndio::ISink* to_sink();
144  virtual sndio::ISource* to_source();
145  virtual sndio::DeviceType type() const;
146  virtual sndio::DeviceState state() const;
147  virtual void pause();
148  virtual bool resume();
149  virtual bool restart();
150  virtual audio::SampleSpec sample_spec() const;
151  virtual core::nanoseconds_t latency() const;
152  virtual bool has_latency() const;
153  virtual bool has_clock() const;
154  virtual void reclock(core::nanoseconds_t timestamp);
155  virtual bool read(audio::Frame&);
156 
157  // Methods of PipelineLoop
158  virtual core::nanoseconds_t timestamp_imp() const;
159  virtual uint64_t tid_imp() const;
160  virtual bool process_subframe_imp(audio::Frame& frame);
161  virtual bool process_task_imp(PipelineTask& task);
162 
163  // Methods for tasks
164  bool task_create_slot_(Task& task);
165  bool task_delete_slot_(Task& task);
166  bool task_query_slot_(Task& task);
167  bool task_add_endpoint_(Task& task);
168 
169  ReceiverSource source_;
170  core::Mutex source_mutex_;
171 
173  core::Ticker::ticks_t ticker_ts_;
174 
175  const bool auto_reclock_;
176 
177  bool valid_;
178 };
179 
180 } // namespace pipeline
181 } // namespace roc
182 
183 #endif // ROC_PIPELINE_RECEIVER_LOOP_H_
Socket address.
Definition: socket_addr.h:26
Audio frame.
Definition: frame.h:25
Sample specification. Describes sample rate and channels.
Definition: sample_spec.h:30
Memory arena interface.
Definition: iarena.h:23
Memory pool interface.
Definition: ipool.h:23
Mutex.
Definition: mutex.h:31
Optionally constructed object.
Definition: optional.h:25
uint64_t ticks_t
Number of ticks.
Definition: ticker.h:26
Packet writer interface.
Definition: iwriter.h:23
Pipeline task scheduler interface. PipelineLoop uses this interface to schedule asynchronous work....
Base class for task-based pipelines.
Base class for pipeline tasks.
Definition: pipeline_task.h:27
bool(ReceiverLoop::* func_)(Task &)
Task implementation method.
Definition: receiver_loop.h:58
ReceiverSlotMetrics * slot_metrics_
Output slot metrics.
Definition: receiver_loop.h:67
address::SocketAddr inbound_address_
Inbound packet address.
Definition: receiver_loop.h:64
ReceiverSlotConfig slot_config_
Slot config.
Definition: receiver_loop.h:61
address::Interface iface_
Interface.
Definition: receiver_loop.h:62
address::Protocol proto_
Protocol.
Definition: receiver_loop.h:63
ReceiverParticipantMetrics * party_metrics_
Output participant metrics.
Definition: receiver_loop.h:68
size_t * party_count_
Input/output participant count.
Definition: receiver_loop.h:69
packet::IWriter * outbound_writer_
Outbound packet writer.
Definition: receiver_loop.h:66
packet::IWriter * inbound_writer_
Inbound packet writer.
Definition: receiver_loop.h:65
Create endpoint on given interface of the slot.
packet::IWriter * get_inbound_writer() const
Get packet writer for inbound packets for the endpoint.
AddEndpoint(SlotHandle slot, address::Interface iface, address::Protocol proto, const address::SocketAddr &inbound_address, packet::IWriter *outbound_writer)
Set task parameters.
SlotHandle get_handle() const
Get created slot handle.
CreateSlot(const ReceiverSlotConfig &slot_config)
Set task parameters.
DeleteSlot(SlotHandle slot)
Set task parameters.
QuerySlot(SlotHandle slot, ReceiverSlotMetrics &slot_metrics, ReceiverParticipantMetrics *party_metrics, size_t *party_count)
Set task parameters.
Subclasses for specific tasks.
Definition: receiver_loop.h:73
Receiver pipeline loop.
Definition: receiver_loop.h:46
bool is_valid() const
Check if the pipeline was successfully constructed.
struct SlotHandle * SlotHandle
Opaque slot handle.
Definition: receiver_loop.h:49
ReceiverLoop(IPipelineTaskScheduler &scheduler, const ReceiverSourceConfig &source_config, const rtp::EncodingMap &encoding_map, core::IPool &packet_pool, core::IPool &packet_buffer_pool, core::IPool &frame_buffer_pool, core::IArena &arena)
Initialize.
sndio::ISource & source()
Get receiver sources.
Receiver source pipeline.
RTP encoding map. Thread-safe. Returned encodings are immutable and can be safely used from any threa...
Definition: encoding_map.h:33
Sink interface.
Definition: isink.h:22
Source interface.
Definition: isource.h:23
Memory arena interface.
Memory pool interface.
Source interface.
Pipeline metrics.
Mutex.
Interface
Interface ID.
Definition: interface.h:19
Protocol
Protocol ID.
Definition: protocol.h:19
nanoseconds_t timestamp(clock_t clock)
Get current timestamp in nanoseconds.
int64_t nanoseconds_t
Nanoseconds.
Definition: time.h:58
DeviceType
Device type.
Definition: device_type.h:19
DeviceState
Device state.
Definition: device_state.h:19
Root namespace.
Optionally constructed object.
Base class for pipelines.
Receiver source pipeline.
Pipeline config.
Commonly used types and functions.
Receiver-side metrics specific to one participant (remote sender).
Definition: metrics.h:54
Parameters of receiver slot.
Definition: config.h:202
Receiver-side metrics of the whole slot.
Definition: metrics.h:66
Parameters of receiver session.
Definition: config.h:184