Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
sender.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/sender.h
10 //! @brief Sender node.
11 
12 #ifndef ROC_NODE_SENDER_H_
13 #define ROC_NODE_SENDER_H_
14 
16 #include "roc_address/interface.h"
17 #include "roc_address/protocol.h"
19 #include "roc_core/hashmap.h"
20 #include "roc_core/mutex.h"
21 #include "roc_core/ref_counted.h"
22 #include "roc_core/scoped_ptr.h"
23 #include "roc_core/slab_pool.h"
24 #include "roc_core/stddefs.h"
25 #include "roc_node/context.h"
26 #include "roc_node/node.h"
27 #include "roc_packet/iwriter.h"
30 
31 namespace roc {
32 namespace node {
33 
34 //! Sender node.
35 class Sender : public Node, private pipeline::IPipelineTaskScheduler {
36 public:
37  //! Slot index.
38  typedef uint64_t slot_index_t;
39 
40  //! Initialize.
41  Sender(Context& context, const pipeline::SenderConfig& pipeline_config);
42 
43  //! Deinitialize.
45 
46  //! Check if successfully constructed.
47  bool is_valid() const;
48 
49  //! Set interface config.
50  bool configure(slot_index_t slot_index,
51  address::Interface iface,
52  const netio::UdpSenderConfig& config);
53 
54  //! Connect to remote endpoint.
55  bool connect(slot_index_t slot_index,
56  address::Interface iface,
57  const address::EndpointUri& uri);
58 
59  //! Remove slot.
60  bool unlink(slot_index_t slot_index);
61 
62  //! Get slot metrics.
63  //! @remarks
64  //! Metrics are written into provided arguments.
65  bool get_metrics(slot_index_t slot_index,
66  pipeline::SenderSlotMetrics& slot_metrics,
67  pipeline::SenderSessionMetrics& sess_metrics);
68 
69  //! Check if there are incomplete or broken slots.
71 
72  //! Check if there are broken slots.
73  bool has_broken();
74 
75  //! Get sender sink.
77 
78 private:
79  struct Port {
81  netio::UdpSenderConfig orig_config;
83  packet::IWriter* writer;
84 
85  Port()
86  : handle(NULL)
87  , writer(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  Port&
127  select_outgoing_port_(Slot& slot, address::Interface, address::AddrFamily family);
128  bool setup_outgoing_port_(Port& port,
129  address::Interface iface,
130  address::AddrFamily family);
131 
132  virtual void schedule_task_processing(pipeline::PipelineLoop&,
133  core::nanoseconds_t delay);
134  virtual void cancel_task_processing(pipeline::PipelineLoop&);
135 
136  core::Mutex mutex_;
137 
138  pipeline::SenderLoop pipeline_;
139  ctl::ControlLoop::Tasks::PipelineProcessing processing_task_;
140 
141  core::SlabPool<Slot> slot_pool_;
142  core::Hashmap<Slot> slot_map_;
143 
144  bool used_interfaces_[address::Iface_Max];
145  address::Protocol used_protocols_[address::Iface_Max];
146 
147  bool valid_;
148 };
149 
150 } // namespace node
151 } // namespace roc
152 
153 #endif // ROC_NODE_SENDER_H_
Allocation policies.
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.
Sender node.
Definition: sender.h:35
bool is_valid() const
Check if successfully constructed.
bool has_broken()
Check if there are broken slots.
bool get_metrics(slot_index_t slot_index, pipeline::SenderSlotMetrics &slot_metrics, pipeline::SenderSessionMetrics &sess_metrics)
Get slot metrics.
bool has_incomplete()
Check if there are incomplete or broken slots.
uint64_t slot_index_t
Slot index.
Definition: sender.h:38
~Sender()
Deinitialize.
sndio::ISink & sink()
Get sender sink.
bool unlink(slot_index_t slot_index)
Remove slot.
bool connect(slot_index_t slot_index, address::Interface iface, const address::EndpointUri &uri)
Connect to remote endpoint.
Sender(Context &context, const pipeline::SenderConfig &pipeline_config)
Initialize.
bool configure(slot_index_t slot_index, address::Interface iface, const netio::UdpSenderConfig &config)
Set interface config.
Packet writer interface.
Definition: iwriter.h:23
Pipeline task scheduler interface. PipelineLoop uses this interface to schedule asynchronous work....
struct SlotHandle * SlotHandle
Opaque slot handle.
Definition: sender_loop.h:47
Sink interface.
Definition: isink.h:22
Node context.
Network endpoint URI.
Intrusive hash table.
Interface ID.
Pipeline task scheduler interface.
Packet writer 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
AddrFamily
Address family.
Definition: addr_family.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.
Base class for object with reference counter.
Unique ownrship pointer.
Sender pipeline loop.
Memory pool.
Commonly used types and functions.
UDP sender parameters.
Sender parameters.
Definition: config.h:102
Metrics of sender session (connection to receiver).
Definition: metrics.h:22
Metrics of sender slot.
Definition: metrics.h:28