Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
receiver.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_node/receiver.h
10 //! @brief Receiver node.
11 
12 #ifndef ROC_NODE_RECEIVER_H_
13 #define ROC_NODE_RECEIVER_H_
14 
16 #include "roc_address/interface.h"
17 #include "roc_address/protocol.h"
18 #include "roc_core/hashmap.h"
19 #include "roc_core/mutex.h"
20 #include "roc_core/ref_counted.h"
21 #include "roc_core/slab_pool.h"
22 #include "roc_core/stddefs.h"
23 #include "roc_ctl/control_loop.h"
24 #include "roc_node/context.h"
25 #include "roc_node/node.h"
28 
29 namespace roc {
30 namespace node {
31 
32 //! Receiver node.
34 public:
35  //! Slot index.
36  typedef uint64_t slot_index_t;
37 
38  //! Initialize.
39  Receiver(Context& context, const pipeline::ReceiverConfig& pipeline_config);
40 
41  //! Deinitialize.
43 
44  //! Check if successfully constructed.
45  bool is_valid();
46 
47  //! Set interface config.
48  bool configure(slot_index_t slot_index,
49  address::Interface iface,
50  const netio::UdpReceiverConfig& config);
51 
52  //! Bind to local endpoint.
53  bool
55 
56  //! Remove slot.
57  bool unlink(slot_index_t slot_index);
58 
59  //! Callback for getting session metrics.
60  typedef void (*sess_metrics_func_t)(
61  const pipeline::ReceiverSessionMetrics& sess_metrics,
62  size_t sess_index,
63  void* sess_arg);
64 
65  //! Get slot metrics.
66  //! @remarks
67  //! Metrics for slot are written into @p slot_metrics.
68  //! Metrics for each session are passed to @p sess_metrics_func.
69  bool get_metrics(slot_index_t slot_index,
70  pipeline::ReceiverSlotMetrics& slot_metrics,
71  sess_metrics_func_t sess_metrics_func,
72  size_t* sess_metrics_size,
73  void* sess_metrics_arg);
74 
75  //! Check if there are broken slots.
76  bool has_broken();
77 
78  //! Get receiver source.
80 
81 private:
82  struct Port {
85 
86  Port()
87  : handle(NULL) {
88  }
89  };
90 
91  struct Slot : core::RefCounted<Slot, core::PoolAllocation>, core::HashmapNode {
92  const slot_index_t index;
94  Port ports[address::Iface_Max];
95  bool broken;
96 
97  Slot(core::IPool& pool,
98  slot_index_t index,
100  : core::RefCounted<Slot, core::PoolAllocation>(pool)
101  , index(index)
102  , handle(handle)
103  , broken(false) {
104  }
105 
106  slot_index_t key() const {
107  return index;
108  }
109 
110  static core::hashsum_t key_hash(slot_index_t index) {
111  return core::hashsum_int(index);
112  }
113 
114  static bool key_equal(slot_index_t index1, slot_index_t index2) {
115  return index1 == index2;
116  }
117  };
118 
119  bool check_compatibility_(address::Interface iface, const address::EndpointUri& uri);
120  void update_compatibility_(address::Interface iface, const address::EndpointUri& uri);
121 
122  core::SharedPtr<Slot> get_slot_(slot_index_t slot_index, bool auto_create);
123  void cleanup_slot_(Slot& slot);
124  void break_slot_(Slot& slot);
125 
126  virtual void schedule_task_processing(pipeline::PipelineLoop&,
127  core::nanoseconds_t delay);
128  virtual void cancel_task_processing(pipeline::PipelineLoop&);
129 
130  core::Mutex mutex_;
131 
132  pipeline::ReceiverLoop pipeline_;
133  ctl::ControlLoop::Tasks::PipelineProcessing processing_task_;
134 
135  core::SlabPool<Slot> slot_pool_;
136  core::Hashmap<Slot> slot_map_;
137 
138  bool used_interfaces_[address::Iface_Max];
139  address::Protocol used_protocols_[address::Iface_Max];
140 
141  core::Array<pipeline::ReceiverSessionMetrics, 8> sess_metrics_;
142 
143  bool valid_;
144 };
145 
146 } // namespace node
147 } // namespace roc
148 
149 #endif // ROC_NODE_RECEIVER_H_
Network endpoint URI.
Definition: endpoint_uri.h:27
Base class for hashmap element.
Definition: hashmap_node.h:27
Memory pool interface.
Definition: ipool.h:23
Base class for object with reference counter.
Definition: ref_counted.h:40
struct PortHandle * PortHandle
Opaque port handle.
Definition: network_loop.h:56
Node context.
Definition: context.h:44
Base class for nodes.
Definition: node.h:23
Context & context()
All nodes hold reference to context.
Receiver node.
Definition: receiver.h:33
bool has_broken()
Check if there are broken slots.
bool get_metrics(slot_index_t slot_index, pipeline::ReceiverSlotMetrics &slot_metrics, sess_metrics_func_t sess_metrics_func, size_t *sess_metrics_size, void *sess_metrics_arg)
Get slot metrics.
bool unlink(slot_index_t slot_index)
Remove slot.
uint64_t slot_index_t
Slot index.
Definition: receiver.h:36
bool configure(slot_index_t slot_index, address::Interface iface, const netio::UdpReceiverConfig &config)
Set interface config.
bool bind(slot_index_t slot_index, address::Interface iface, address::EndpointUri &uri)
Bind to local endpoint.
~Receiver()
Deinitialize.
void(* sess_metrics_func_t)(const pipeline::ReceiverSessionMetrics &sess_metrics, size_t sess_index, void *sess_arg)
Callback for getting session metrics.
Definition: receiver.h:60
bool is_valid()
Check if successfully constructed.
sndio::ISource & source()
Get receiver source.
Receiver(Context &context, const pipeline::ReceiverConfig &pipeline_config)
Initialize.
Pipeline task scheduler interface. PipelineLoop uses this interface to schedule asynchronous work....
struct SlotHandle * SlotHandle
Opaque slot handle.
Definition: receiver_loop.h:50
struct SlotHandle * SlotHandle
Opaque slot handle.
Definition: sender_loop.h:47
Source interface.
Definition: isource.h:23
Node context.
Control loop thread.
Network endpoint URI.
Intrusive hash table.
Interface ID.
Pipeline task scheduler interface.
Mutex.
Interface
Interface ID.
Definition: interface.h:19
@ Iface_Max
Number of interfaces.
Definition: interface.h:36
Protocol
Protocol ID.
Definition: protocol.h:19
hashsum_t hashsum_int(int16_t)
Compute hash of 16-bit integer.
int64_t nanoseconds_t
Nanoseconds.
Definition: time.h:58
size_t hashsum_t
Hash type.
Definition: hashsum.h:21
Root namespace.
Base class for nodes.
Protocol ID.
Receiver pipeline loop.
Base class for object with reference counter.
Memory pool.
Commonly used types and functions.
UDP receiver parameters.
Receiver parameters.
Definition: config.h:238
Metrics of receiver session (connection from sender).
Definition: metrics.h:38
Metrics of receiver slot.
Definition: metrics.h:44