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 
16 #include "roc_core/iarena.h"
17 #include "roc_core/mutex.h"
18 #include "roc_core/optional.h"
19 #include "roc_core/stddefs.h"
21 #include "roc_pipeline/config.h"
22 #include "roc_pipeline/metrics.h"
25 #include "roc_sndio/isource.h"
26 
27 namespace roc {
28 namespace pipeline {
29 
30 //! Receiver pipeline loop.
31 //!
32 //! This class acts as a task-based facade for the receiver pipeline subsystem
33 //! of roc_pipeline module (ReceiverSource, ReceiverSlot, ReceiverEndpoint,
34 //! ReceiverSessionGroup, ReceiverSession).
35 //!
36 //! It provides two interfaces:
37 //!
38 //! - sndio::ISource - can be used to retrieve samples from the pipeline
39 //! (should be used from sndio thread)
40 //!
41 //! - PipelineLoop - can be used to schedule tasks on the pipeline
42 //! (can be used from any thread)
43 //!
44 //! @note
45 //! Private inheritance from ISource is used to decorate actual implementation
46 //! of ISource - ReceiverSource, in order to integrate it with PipelineLoop.
47 class ReceiverLoop : public PipelineLoop, private sndio::ISource {
48 public:
49  //! Opaque slot handle.
50  typedef struct SlotHandle* SlotHandle;
51 
52  //! Base task class.
53  class Task : public PipelineTask {
54  protected:
55  friend class ReceiverLoop;
56 
57  Task();
58 
59  bool (ReceiverLoop::*func_)(Task&); //!< Task implementation method.
60 
61  ReceiverSlot* slot_; //!< Slot.
62  address::Interface iface_; //!< Interface.
63  address::Protocol proto_; //!< Protocol.
64  packet::IWriter* writer_; //!< Packet writer.
65  ReceiverSlotMetrics* slot_metrics_; //!< Output for slot metrics.
66  ReceiverSessionMetrics* sess_metrics_; //!< Output for session metrics.
67  size_t* sess_metrics_size_; //!< Input/output session metrics size.
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.
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  ReceiverSlotMetrics& slot_metrics,
98  ReceiverSessionMetrics* sess_metrics,
99  size_t* sess_metrics_size);
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 
113  //! Get packet writer for the endpoint.
114  //! @remarks
115  //! The returned writer may be used from any thread.
117  };
118  };
119 
120  //! Initialize.
122  const ReceiverConfig& 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 receiver sources.
133  //! @remarks
134  //! Samples received from remote peers become available in this source.
136 
137 private:
138  // Methods of sndio::ISource
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 reclock(core::nanoseconds_t timestamp);
149  virtual bool read(audio::Frame&);
150 
151  // Methods of PipelineLoop
152  virtual core::nanoseconds_t timestamp_imp() const;
153  virtual uint64_t tid_imp() const;
154  virtual bool process_subframe_imp(audio::Frame& frame);
155  virtual bool process_task_imp(PipelineTask& task);
156 
157  // Methods for tasks
158  bool task_create_slot_(Task& task);
159  bool task_delete_slot_(Task& task);
160  bool task_query_slot_(Task& task);
161  bool task_add_endpoint_(Task& task);
162 
163  ReceiverSource source_;
164  core::Mutex source_mutex_;
165 
167  core::Ticker::ticks_t ticker_ts_;
168 
169  bool auto_reclock_;
170 
171  bool valid_;
172 };
173 
174 } // namespace pipeline
175 } // namespace roc
176 
177 #endif // ROC_PIPELINE_RECEIVER_LOOP_H_
Buffer factory.
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
bool(ReceiverLoop::* func_)(Task &)
Task implementation method.
Definition: receiver_loop.h:59
ReceiverSlotMetrics * slot_metrics_
Output for slot metrics.
Definition: receiver_loop.h:65
packet::IWriter * writer_
Packet writer.
Definition: receiver_loop.h:64
address::Interface iface_
Interface.
Definition: receiver_loop.h:62
address::Protocol proto_
Protocol.
Definition: receiver_loop.h:63
size_t * sess_metrics_size_
Input/output session metrics size.
Definition: receiver_loop.h:67
ReceiverSessionMetrics * sess_metrics_
Output for session metrics.
Definition: receiver_loop.h:66
Create endpoint on given interface of the slot.
AddEndpoint(SlotHandle slot, address::Interface iface, address::Protocol proto)
Set task parameters.
packet::IWriter * get_writer() const
Get packet writer for the endpoint.
SlotHandle get_handle() const
Get created slot handle.
DeleteSlot(SlotHandle slot)
Set task parameters.
QuerySlot(SlotHandle slot, ReceiverSlotMetrics &slot_metrics, ReceiverSessionMetrics *sess_metrics, size_t *sess_metrics_size)
Set task parameters.
Subclasses for specific tasks.
Definition: receiver_loop.h:71
Receiver pipeline loop.
Definition: receiver_loop.h:47
bool is_valid() const
Check if the pipeline was successfully constructed.
ReceiverLoop(IPipelineTaskScheduler &scheduler, const ReceiverConfig &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.
struct SlotHandle * SlotHandle
Opaque slot handle.
Definition: receiver_loop.h:50
sndio::ISource & source()
Get receiver sources.
Receiver source pipeline.
RTP payload format map. Thread-safe. Returned formats are immutable and can be safely used from any t...
Definition: format_map.h:33
Source interface.
Definition: isource.h:23
Memory arena interface.
Source interface.
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.
Packet factory.
Base class for pipelines.
Receiver source pipeline.
Pipeline config.
Pipeline metrics.
Commonly used types and functions.
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