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 
16 #include "roc_core/iarena.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  //! Opaque endpoint handle.
50  typedef struct EndpointHandle* EndpointHandle;
51 
52  //! Base task class.
53  class Task : public PipelineTask {
54  protected:
55  friend class SenderLoop;
56 
57  Task();
58 
59  bool (SenderLoop::*func_)(Task&); //!< Task implementation method.
60 
61  SenderSlot* slot_; //!< Slot.
62  SenderEndpoint* endpoint_; //!< Endpoint.
63  address::Interface iface_; //!< Interface.
64  address::Protocol proto_; //!< Protocol.
65  address::SocketAddr address_; //!< Destination address.
66  packet::IWriter* writer_; //!< Destination writer.
67  SenderSlotMetrics* slot_metrics_; //!< Output for slot metrics.
68  SenderSessionMetrics* sess_metrics_; //!< Output for session metrics.
69  };
70 
71  //! Subclasses for specific tasks.
72  class Tasks {
73  public:
74  //! Create new slot.
75  class CreateSlot : public Task {
76  public:
77  //! Set task parameters.
79 
80  //! Get created slot handle.
82  };
83 
84  //! Delete existing slot.
85  class DeleteSlot : public Task {
86  public:
87  //! Set task parameters.
89  };
90 
91  //! Query slot metrics.
92  class QuerySlot : public Task {
93  public:
94  //! Set task parameters.
95  //! @remarks
96  //! Metrics are written to provided structs.
98  SenderSlotMetrics& slot_metrics,
99  SenderSessionMetrics* sess_metrics);
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& dest_address,
113  packet::IWriter& dest_writer);
114 
115  //! Get created endpoint handle.
117  };
118  };
119 
120  //! Initialize.
122  const SenderConfig& config,
123  const rtp::FormatMap& format_map,
124  packet::PacketFactory& packet_factory,
125  core::BufferFactory<uint8_t>& byte_buffer_factory,
126  core::BufferFactory<audio::sample_t>& sample_buffer_factory,
127  core::IArena& arena);
128 
129  //! Check if the pipeline was successfully constructed.
130  bool is_valid() const;
131 
132  //! Get sender sink.
133  //! @remarks
134  //! Samples written to the sink are sent to remote peers.
136 
137 private:
138  // Methods of sndio::ISink
139  virtual sndio::DeviceType type() const;
140  virtual sndio::DeviceState state() const;
141  virtual void pause();
142  virtual bool resume();
143  virtual bool restart();
144  virtual audio::SampleSpec sample_spec() const;
145  virtual core::nanoseconds_t latency() const;
146  virtual bool has_latency() const;
147  virtual bool has_clock() const;
148  virtual void write(audio::Frame& frame);
149 
150  // Methods of PipelineLoop
151  virtual core::nanoseconds_t timestamp_imp() const;
152  virtual uint64_t tid_imp() const;
153  virtual bool process_subframe_imp(audio::Frame&);
154  virtual bool process_task_imp(PipelineTask&);
155 
156  // Methods for tasks
157  bool task_create_slot_(Task&);
158  bool task_delete_slot_(Task&);
159  bool task_query_slot_(Task&);
160  bool task_add_endpoint_(Task&);
161 
162  SenderSink sink_;
163  core::Mutex sink_mutex_;
164 
166  core::Ticker::ticks_t ticker_ts_;
167 
168  bool auto_cts_;
169 
170  bool valid_;
171 };
172 
173 } // namespace pipeline
174 } // namespace roc
175 
176 #endif // ROC_PIPELINE_SENDER_LOOP_H_
Buffer factory.
Socket address.
Definition: socket_addr.h:26
Audio frame.
Definition: frame.h:25
Sample specification. Describes sample rate and channels.
Definition: sample_spec.h:26
Memory arena interface.
Definition: iarena.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
Sender endpoint sub-pipeline.
SenderSlotMetrics * slot_metrics_
Output for slot metrics.
Definition: sender_loop.h:67
address::Protocol proto_
Protocol.
Definition: sender_loop.h:64
SenderSessionMetrics * sess_metrics_
Output for session metrics.
Definition: sender_loop.h:68
bool(SenderLoop::* func_)(Task &)
Task implementation method.
Definition: sender_loop.h:59
address::SocketAddr address_
Destination address.
Definition: sender_loop.h:65
address::Interface iface_
Interface.
Definition: sender_loop.h:63
packet::IWriter * writer_
Destination writer.
Definition: sender_loop.h:66
SenderEndpoint * endpoint_
Endpoint.
Definition: sender_loop.h:62
Create endpoint on given interface of the slot.
Definition: sender_loop.h:103
EndpointHandle get_handle() const
Get created endpoint handle.
AddEndpoint(SlotHandle slot, address::Interface iface, address::Protocol proto, const address::SocketAddr &dest_address, packet::IWriter &dest_writer)
Set task parameters.
SlotHandle get_handle() const
Get created slot handle.
DeleteSlot(SlotHandle slot)
Set task parameters.
QuerySlot(SlotHandle slot, SenderSlotMetrics &slot_metrics, SenderSessionMetrics *sess_metrics)
Set task parameters.
Subclasses for specific tasks.
Definition: sender_loop.h:72
Sender pipeline loop.
Definition: sender_loop.h:44
sndio::ISink & sink()
Get sender sink.
struct EndpointHandle * EndpointHandle
Opaque endpoint handle.
Definition: sender_loop.h:50
SenderLoop(IPipelineTaskScheduler &scheduler, const SenderConfig &config, const rtp::FormatMap &format_map, packet::PacketFactory &packet_factory, core::BufferFactory< uint8_t > &byte_buffer_factory, core::BufferFactory< audio::sample_t > &sample_buffer_factory, 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:47
RTP payload format map. Thread-safe. Returned formats are immutable and can be safely used from any t...
Definition: format_map.h:33
Sink interface.
Definition: isink.h:22
Memory arena interface.
Sink interface.
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.
Pipeline metrics.
Sender sink pipeline.
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
Ticker.