Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
receiver.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Roc 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.h
10 //! @brief Receiver pipeline.
11 
12 #ifndef ROC_PIPELINE_RECEIVER_H_
13 #define ROC_PIPELINE_RECEIVER_H_
14 
15 #include "roc_audio/ireader.h"
16 #include "roc_audio/mixer.h"
18 #include "roc_core/buffer_pool.h"
19 #include "roc_core/cond.h"
20 #include "roc_core/iallocator.h"
21 #include "roc_core/list.h"
22 #include "roc_core/mutex.h"
23 #include "roc_core/noncopyable.h"
24 #include "roc_core/unique_ptr.h"
25 #include "roc_fec/codec_map.h"
26 #include "roc_packet/ireader.h"
27 #include "roc_packet/iwriter.h"
28 #include "roc_packet/packet_pool.h"
29 #include "roc_pipeline/config.h"
32 #include "roc_rtp/format_map.h"
33 #include "roc_sndio/isource.h"
34 
35 namespace roc {
36 namespace pipeline {
37 
38 //! Receiver pipeline.
39 class Receiver : public sndio::ISource,
40  public packet::IWriter,
41  public core::NonCopyable<> {
42 public:
43  //! Initialize.
44  Receiver(const ReceiverConfig& config,
45  const fec::CodecMap& codec_map,
46  const rtp::FormatMap& format_map,
47  packet::PacketPool& packet_pool,
48  core::BufferPool<uint8_t>& byte_buffer_pool,
49  core::BufferPool<audio::sample_t>& sample_buffer_pool,
50  core::IAllocator& allocator);
51 
52  //! Check if the pipeline was successfully constructed.
53  bool valid();
54 
55  //! Add receiving port.
56  bool add_port(const PortConfig& config);
57 
58  //! Iterate added ports.
59  void iterate_ports(void (*fn)(void*, const PortConfig&), void* arg) const;
60 
61  //! Get number of alive sessions.
62  size_t num_sessions() const;
63 
64  //! Get current receiver state.
65  virtual State state() const;
66 
67  //! Wait until the receiver status becomes active.
68  virtual void wait_active() const;
69 
70  //! Get source sample rate.
71  virtual size_t sample_rate() const;
72 
73  //! Check if the source has own clock.
74  virtual bool has_clock() const;
75 
76  //! Write packet.
77  virtual void write(const packet::PacketPtr&);
78 
79  //! Read frame.
80  virtual bool read(audio::Frame&);
81 
82 private:
83  State state_() const;
84 
85  void prepare_();
86 
87  void fetch_packets_();
88 
89  bool parse_packet_(const packet::PacketPtr& packet);
90  bool route_packet_(const packet::PacketPtr& packet);
91 
92  bool can_create_session_(const packet::PacketPtr& packet);
93 
94  bool create_session_(const packet::PacketPtr& packet);
95  void remove_session_(ReceiverSession& sess);
96 
97  void update_sessions_();
98 
99  ReceiverSessionConfig make_session_config_(const packet::PacketPtr& packet) const;
100 
101  const fec::CodecMap& codec_map_;
102  const rtp::FormatMap& format_map_;
103 
104  packet::PacketPool& packet_pool_;
105  core::BufferPool<uint8_t>& byte_buffer_pool_;
106  core::BufferPool<audio::sample_t>& sample_buffer_pool_;
107  core::IAllocator& allocator_;
108 
110  core::List<ReceiverSession> sessions_;
111 
113 
114  core::Ticker ticker_;
115 
118 
119  audio::IReader* audio_reader_;
120 
121  ReceiverConfig config_;
122 
123  packet::timestamp_t timestamp_;
124  size_t num_channels_;
125 
126  core::Mutex control_mutex_;
127  core::Mutex pipeline_mutex_;
128  core::Cond active_cond_;
129 };
130 
131 } // namespace pipeline
132 } // namespace roc
133 
134 #endif // ROC_PIPELINE_RECEIVER_H_
Receiver parameters.
Definition: config.h:196
Port parameters.
Definition: config.h:56
Memory allocator interface.
Definition: iallocator.h:23
Pipeline config.
RTP payload format map.
Packet reader interface.
State
Source state.
Definition: isource.h:32
virtual State state() const
Get current receiver state.
Packet pool.
Condition variable.
Definition: cond.h:25
Root namespace.
void iterate_ports(void(*fn)(void *, const PortConfig &), void *arg) const
Iterate added ports.
virtual bool read(audio::Frame &)
Read frame.
Intrusive doubly-linked list.
Definition: list.h:31
virtual void write(const packet::PacketPtr &)
Write packet.
Receiver session pipeline.
Mutex.
Definition: mutex.h:27
Audio reader interface.
Packet writer interface.
uint32_t timestamp_t
Audio packet timestamp.
Definition: units.h:46
Audio frame.
Definition: frame.h:22
Receiver port pipeline.
Receiver pipeline.
Definition: receiver.h:39
Packet writer interface.
Definition: iwriter.h:21
Intrusive doubly-linked list.
Receiver session pipeline.
Source interface.
Base class for non-copyable objects.
Definition: noncopyable.h:23
FEC codec map.
RTP payload format map.
Definition: format_map.h:22
Receiver session parameters.
Definition: config.h:122
Mutex.
size_t num_sessions() const
Get number of alive sessions.
virtual void wait_active() const
Wait until the receiver status becomes active.
Audio reader interface.
Definition: ireader.h:22
bool add_port(const PortConfig &config)
Add receiving port.
virtual size_t sample_rate() const
Get source sample rate.
Mixer.
Buffer pool.
Condition variable.
Receiver(const ReceiverConfig &config, const fec::CodecMap &codec_map, const rtp::FormatMap &format_map, packet::PacketPool &packet_pool, core::BufferPool< uint8_t > &byte_buffer_pool, core::BufferPool< audio::sample_t > &sample_buffer_pool, core::IAllocator &allocator)
Initialize.
Unique ownrship pointer.
Non-copyable object.
Poison reader.
FEC codec map.
Definition: codec_map.h:26
virtual bool has_clock() const
Check if the source has own clock.
Source interface.
Definition: isource.h:21
bool valid()
Check if the pipeline was successfully constructed.
Ticker.
Definition: ticker.h:23
Memory allocator interface.