Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
writer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 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_fec/writer.h
10 //! @brief FEC writer.
11 
12 #ifndef ROC_FEC_WRITER_H_
13 #define ROC_FEC_WRITER_H_
14 
15 #include "roc_core/array.h"
16 #include "roc_core/buffer_pool.h"
17 #include "roc_core/iallocator.h"
18 #include "roc_core/noncopyable.h"
19 #include "roc_core/slice.h"
20 #include "roc_fec/iblock_encoder.h"
21 #include "roc_packet/icomposer.h"
22 #include "roc_packet/iwriter.h"
23 #include "roc_packet/packet.h"
24 #include "roc_packet/packet_pool.h"
25 
26 namespace roc {
27 namespace fec {
28 
29 //! FEC writer parameters.
30 struct WriterConfig {
31  //! Number of data packets in block.
33 
34  //! Number of FEC packets in block.
36 
37  WriterConfig()
38  : n_source_packets(20)
39  , n_repair_packets(10) {
40  }
41 };
42 
43 //! FEC writer.
44 class Writer : public packet::IWriter, public core::NonCopyable<> {
45 public:
46  //! Initialize.
47  //!
48  //! @b Parameters
49  //! - @p config contains FEC scheme parameters
50  //! - @p encoder is used to encode repair packets
51  //! - @p writer is used to write source and repair packets
52  //! - @p source_composer is used to format source packets
53  //! - @p repair_composer is used to format repair packets
54  //! - @p packet_pool is used to allocate repair packets
55  //! - @p buffer_pool is used to allocate buffers for repair packets
56  //! - @p allocator is used to initialize a packet array
57  Writer(const WriterConfig& config,
58  packet::FECScheme fec_scheme,
59  IBlockEncoder& encoder,
60  packet::IWriter& writer,
61  packet::IComposer& source_composer,
62  packet::IComposer& repair_composer,
63  packet::PacketPool& packet_pool,
64  core::BufferPool<uint8_t>& buffer_pool,
65  core::IAllocator& allocator);
66 
67  //! Check if object is successfully constructed.
68  bool valid() const;
69 
70  //! Check if writer is still working.
71  bool alive() const;
72 
73  //! Set number of source packets per block.
74  bool resize(size_t sblen, size_t rblen);
75 
76  //! Write packet.
77  //! @remarks
78  //! - writes the given source packet to the output writer
79  //! - generates repair packets and also writes them to the output writer
80  virtual void write(const packet::PacketPtr&);
81 
82 private:
83  bool begin_block_(const packet::PacketPtr& pp);
84  void end_block_();
85  void next_block_();
86 
87  bool apply_sizes_(size_t sblen, size_t rblen, size_t payload_size);
88 
89  void write_source_packet_(const packet::PacketPtr&);
90  void make_repair_packets_();
91  packet::PacketPtr make_repair_packet_(packet::seqnum_t n);
92  void encode_repair_packets_();
93  void compose_repair_packets_();
94  void write_repair_packets_();
95  void fill_packet_fec_fields_(const packet::PacketPtr& packet, packet::seqnum_t n);
96 
97  void validate_fec_packet_(const packet::PacketPtr&);
98  bool validate_source_packet_(const packet::PacketPtr&);
99 
100  size_t cur_sblen_;
101  size_t next_sblen_;
102 
103  size_t cur_rblen_;
104  size_t next_rblen_;
105 
106  size_t cur_payload_size_;
107 
108  IBlockEncoder& encoder_;
109  packet::IWriter& writer_;
110 
111  packet::IComposer& source_composer_;
112  packet::IComposer& repair_composer_;
113 
114  packet::PacketPool& packet_pool_;
115  core::BufferPool<uint8_t>& buffer_pool_;
116 
117  core::Array<packet::PacketPtr> repair_block_;
118 
119  bool first_packet_;
120 
121  packet::blknum_t cur_sbn_;
122  packet::seqnum_t cur_block_repair_sn_;
123 
124  size_t cur_packet_;
125 
126  const packet::FECScheme fec_scheme_;
127 
128  bool valid_;
129  bool alive_;
130 };
131 
132 } // namespace fec
133 } // namespace roc
134 
135 #endif // ROC_FEC_WRITER_H_
Memory allocator interface.
Definition: iallocator.h:23
FEC writer parameters.
Definition: writer.h:30
FEC writer.
Definition: writer.h:44
Packet pool.
Root namespace.
uint16_t blknum_t
FEC block number in a packet stream.
Definition: units.h:91
Dynamic array.
Packet writer interface.
Packet composer interface.
Definition: icomposer.h:22
Packet composer interface.
Packet writer interface.
Definition: iwriter.h:21
Base class for non-copyable objects.
Definition: noncopyable.h:23
size_t n_source_packets
Number of data packets in block.
Definition: writer.h:32
FEC block encoder interface.
size_t n_repair_packets
Number of FEC packets in block.
Definition: writer.h:35
Slice.
FECScheme
FECFRAME scheme.
Definition: fec.h:23
Packet.
Buffer pool.
Non-copyable object.
FEC block encoder interface.
Memory allocator interface.
uint16_t seqnum_t
Packet sequence number in packet stream.
Definition: units.h:25