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