Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
sender_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/sender_loop.h
10 //! @brief Sender pipeline loop.
11 
12 #ifndef ROC_PIPELINE_SENDER_LOOP_H_
13 #define ROC_PIPELINE_SENDER_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/ticker.h"
19 #include "roc_pipeline/config.h"
20 #include "roc_pipeline/metrics.h"
23 #include "roc_sndio/isink.h"
24 
25 namespace roc {
26 namespace pipeline {
27 
28 //! Sender pipeline loop.
29 //
30 //! This class acts as a task-based facade for the sender pipeline subsystem of
31 //! roc_pipeline module (SenderSink, SenderSlot, SenderEndpoint, SenderSession).
32 //!
33 //! It provides two interfaces:
34 //!
35 //! - sndio::ISink - can be used to pass samples to the pipeline
36 //! (should be used from sndio thread)
37 //!
38 //! - PipelineLoop - can be used to schedule tasks on the pipeline
39 //! (can be used from any thread)
40 //!
41 //! @note
42 //! Private inheritance from ISink is used to decorate actual implementation
43 //! of ISink - SenderSource, in order to integrate it with PipelineLoop.
44 class SenderLoop : public PipelineLoop, private sndio::ISink {
45 public:
46  //! Opaque slot handle.
47  typedef struct SlotHandle* SlotHandle;
48 
49  //! Base task class.
50  class Task : public PipelineTask {
51  protected:
52  friend class SenderLoop;
53 
54  Task();
55 
56  bool (SenderLoop::*func_)(Task&); //!< Task implementation method.
57 
58  SenderSlot* slot_; //!< Slot.
59  SenderSlotConfig slot_config_; //!< Slot config.
60  address::Interface iface_; //!< Interface.
61  address::Protocol proto_; //!< Protocol.
62  address::SocketAddr outbound_address_; //!< Destination address.
63  packet::IWriter* outbound_writer_; //!< Destination packet writer.
64  packet::IWriter* inbound_writer_; //!< Inbound packet writer.
65  SenderSlotMetrics* slot_metrics_; //!< Output slot metrics.
66  SenderParticipantMetrics* party_metrics_; //!< Output participant metrics.
67  size_t* party_count_; //!< Input/output participant count.
68  };
69 
70  //! Subclasses for specific tasks.
71  class Tasks {
72  public:
73  //! Create new slot.
74  class CreateSlot : public Task {
75  public:
76  //! Set task parameters.
77  CreateSlot(const SenderSlotConfig& slot_config);
78 
79  //! Get created slot handle.
81  };
82 
83  //! Delete existing slot.
84  class DeleteSlot : public Task {
85  public:
86  //! Set task parameters.
88  };
89 
90  //! Query slot metrics.
91  class QuerySlot : public Task {
92  public:
93  //! Set task parameters.
94  //! @remarks
95  //! Metrics are written to provided structs.
97  SenderSlotMetrics& slot_metrics,
98  SenderParticipantMetrics* party_metrics,
99  size_t* party_count);
100  };
101 
102  //! Create endpoint on given interface of the slot.
103  class AddEndpoint : public Task {
104  public:
105  //! Set task parameters.
106  //! @remarks
107  //! Each slot can have one source and zero or one repair endpoint.
108  //! The protocols of endpoints in one slot should be compatible.
110  address::Interface iface,
111  address::Protocol proto,
112  const address::SocketAddr& outbound_address,
113  packet::IWriter& outbound_writer);
114 
115  //! Get packet writer for inbound packets for the endpoint.
116  //! @remarks
117  //! The returned writer may be used from any thread.
119  };
120  };
121 
122  //! Initialize.
124  const SenderSinkConfig& sink_config,
125  const rtp::EncodingMap& encoding_map,
126  core::IPool& packet_pool,
127  core::IPool& packet_buffer_pool,
128  core::IPool& frame_buffer_pool,
129  core::IArena& arena);
130 
131  //! Check if the pipeline was successfully constructed.
132  bool is_valid() const;
133 
134  //! Get sender sink.
135  //! @remarks
136  //! Samples written to the sink are sent to remote peers.
138 
139 private:
140  // Methods of sndio::ISink
141  virtual sndio::ISink* to_sink();
142  virtual sndio::ISource* to_source();
143  virtual sndio::DeviceType type() const;
144  virtual sndio::DeviceState state() const;
145  virtual void pause();
146  virtual bool resume();
147  virtual bool restart();
148  virtual audio::SampleSpec sample_spec() const;
149  virtual core::nanoseconds_t latency() const;
150  virtual bool has_latency() const;
151  virtual bool has_clock() const;
152  virtual void write(audio::Frame& frame);
153 
154  // Methods of PipelineLoop
155  virtual core::nanoseconds_t timestamp_imp() const;
156  virtual uint64_t tid_imp() const;
157  virtual bool process_subframe_imp(audio::Frame&);
158  virtual bool process_task_imp(PipelineTask&);
159 
160  // Methods for tasks
161  bool task_create_slot_(Task&);
162  bool task_delete_slot_(Task&);
163  bool task_query_slot_(Task&);
164  bool task_add_endpoint_(Task&);
165 
166  SenderSink sink_;
167  core::Mutex sink_mutex_;
168 
170  core::Ticker::ticks_t ticker_ts_;
171 
172  const bool auto_duration_;
173  const bool auto_cts_;
174 
175  const audio::SampleSpec sample_spec_;
176 
177  bool valid_;
178 };
179 
180 } // namespace pipeline
181 } // namespace roc
182 
183 #endif // ROC_PIPELINE_SENDER_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
SenderSlotMetrics * slot_metrics_
Output slot metrics.
Definition: sender_loop.h:65
size_t * party_count_
Input/output participant count.
Definition: sender_loop.h:67
address::SocketAddr outbound_address_
Destination address.
Definition: sender_loop.h:62
address::Protocol proto_
Protocol.
Definition: sender_loop.h:61
SenderSlotConfig slot_config_
Slot config.
Definition: sender_loop.h:59
SenderParticipantMetrics * party_metrics_
Output participant metrics.
Definition: sender_loop.h:66
packet::IWriter * outbound_writer_
Destination packet writer.
Definition: sender_loop.h:63
bool(SenderLoop::* func_)(Task &)
Task implementation method.
Definition: sender_loop.h:56
address::Interface iface_
Interface.
Definition: sender_loop.h:60
packet::IWriter * inbound_writer_
Inbound packet writer.
Definition: sender_loop.h:64
Create endpoint on given interface of the slot.
Definition: sender_loop.h:103
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 &outbound_address, packet::IWriter &outbound_writer)
Set task parameters.
CreateSlot(const SenderSlotConfig &slot_config)
Set task parameters.
SlotHandle get_handle() const
Get created slot handle.
DeleteSlot(SlotHandle slot)
Set task parameters.
QuerySlot(SlotHandle slot, SenderSlotMetrics &slot_metrics, SenderParticipantMetrics *party_metrics, size_t *party_count)
Set task parameters.
Subclasses for specific tasks.
Definition: sender_loop.h:71
Sender pipeline loop.
Definition: sender_loop.h:44
sndio::ISink & sink()
Get sender sink.
SenderLoop(IPipelineTaskScheduler &scheduler, const SenderSinkConfig &sink_config, const rtp::EncodingMap &encoding_map, core::IPool &packet_pool, core::IPool &packet_buffer_pool, core::IPool &frame_buffer_pool, core::IArena &arena)
Initialize.
bool is_valid() const
Check if the pipeline was successfully constructed.
struct SlotHandle * SlotHandle
Opaque slot handle.
Definition: sender_loop.h:47
Sender sink pipeline.
Definition: sender_sink.h:43
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.
Sink interface.
Pipeline metrics.
Mutex.
Interface
Interface ID.
Definition: interface.h:19
Protocol
Protocol ID.
Definition: protocol.h:19
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.
Base class for pipelines.
Pipeline config.
Sender sink pipeline.
Sender-side metrics specific to one participant (remote receiver).
Definition: metrics.h:24
Parameters of sender sink and sender session.
Definition: config.h:58
Parameters of sender slot.
Definition: config.h:115
Sender-side metrics of the whole slot.
Definition: metrics.h:36
Ticker.