Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
reader.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/reader.h
10 //! @brief FEC reader.
11 
12 #ifndef ROC_FEC_READER_H_
13 #define ROC_FEC_READER_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_decoder.h"
20 #include "roc_packet/iparser.h"
21 #include "roc_packet/ireader.h"
22 #include "roc_packet/packet.h"
25 
26 namespace roc {
27 namespace fec {
28 
29 //! FEC reader parameters.
30 struct ReaderConfig {
31  //! Maximum allowed source block number jump.
32  size_t max_sbn_jump;
33 
34  ReaderConfig()
35  : max_sbn_jump(100) {
36  }
37 };
38 
39 //! FEC reader.
40 class Reader : public packet::IReader, public core::NonCopyable<> {
41 public:
42  //! Initialize.
43  //!
44  //! @b Parameters
45  //! - @p config contains FEC scheme parameters
46  //! - @p decoder specifies FEC codec implementation;
47  //! - @p source_reader specifies input queue with data packets;
48  //! - @p repair_reader specifies input queue with FEC packets;
49  //! - @p parser specifies packet parser for restored packets.
50  //! - @p arena is used to initialize a packet array
51  Reader(const ReaderConfig& config,
52  packet::FecScheme fec_scheme,
53  IBlockDecoder& decoder,
54  packet::IReader& source_reader,
55  packet::IReader& repair_reader,
56  packet::IParser& parser,
57  packet::PacketFactory& packet_factory,
58  core::IArena& arena);
59 
60  //! Check if object is successfully constructed.
61  bool is_valid() const;
62 
63  //! Did decoder catch block beginning?
64  bool is_started() const;
65 
66  //! Is decoder alive?
67  bool is_alive() const;
68 
69  //! Read packet.
70  //! @remarks
71  //! When a packet loss is detected, try to restore it from repair packets.
73 
74 private:
76 
77  bool try_start_();
78  status::StatusCode get_next_packet_(packet::PacketPtr&);
79 
80  void next_block_();
81  void try_repair_();
82 
83  packet::PacketPtr parse_repaired_packet_(const core::Slice<uint8_t>& buffer);
84 
85  status::StatusCode fetch_all_packets_();
87 
88  void fill_block_();
89  void fill_source_block_();
90  void fill_repair_block_();
91 
92  bool process_source_packet_(const packet::PacketPtr&);
93  bool process_repair_packet_(const packet::PacketPtr&);
94 
95  bool validate_fec_packet_(const packet::PacketPtr&);
96  bool validate_sbn_sequence_(const packet::PacketPtr&);
97 
98  bool validate_incoming_source_packet_(const packet::PacketPtr&);
99  bool validate_incoming_repair_packet_(const packet::PacketPtr&);
100 
101  bool can_update_payload_size_(size_t);
102  bool can_update_source_block_size_(size_t);
103  bool can_update_repair_block_size_(size_t);
104 
105  bool update_payload_size_(size_t);
106  bool update_source_block_size_(size_t);
107  bool update_repair_block_size_(size_t);
108 
109  void drop_repair_packets_from_prev_blocks_();
110 
111  IBlockDecoder& decoder_;
112 
113  packet::IReader& source_reader_;
114  packet::IReader& repair_reader_;
115  packet::IParser& parser_;
116  packet::PacketFactory& packet_factory_;
117 
118  packet::SortedQueue source_queue_;
119  packet::SortedQueue repair_queue_;
120 
121  core::Array<packet::PacketPtr> source_block_;
122  core::Array<packet::PacketPtr> repair_block_;
123 
124  bool valid_;
125 
126  bool alive_;
127  bool started_;
128  bool can_repair_;
129 
130  size_t next_packet_;
131  packet::blknum_t cur_sbn_;
132 
133  size_t payload_size_;
134 
135  bool source_block_resized_;
136  bool repair_block_resized_;
137  bool payload_resized_;
138 
139  unsigned n_packets_;
140 
141  const size_t max_sbn_jump_;
142  const packet::FecScheme fec_scheme_;
143 };
144 
145 } // namespace fec
146 } // namespace roc
147 
148 #endif // ROC_FEC_READER_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 decoder interface.
FEC reader.
Definition: reader.h:40
bool is_valid() const
Check if object is successfully constructed.
bool is_started() const
Did decoder catch block beginning?
virtual ROC_ATTR_NODISCARD status::StatusCode read(packet::PacketPtr &)
Read packet.
bool is_alive() const
Is decoder alive?
Reader(const ReaderConfig &config, packet::FecScheme fec_scheme, IBlockDecoder &decoder, packet::IReader &source_reader, packet::IReader &repair_reader, packet::IParser &parser, packet::PacketFactory &packet_factory, core::IArena &arena)
Initialize.
Packet parser interface.
Definition: iparser.h:22
Packet reader interface.
Definition: ireader.h:23
Packet writer interface.
Definition: iwriter.h:23
Sorted packet queue.
Definition: sorted_queue.h:27
Memory arena interface.
FEC block decoder interface.
Packet parser interface.
Packet reader interface.
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.
Sorted packet queue.
StatusCode
Status code.
Definition: status_code.h:19
FEC reader parameters.
Definition: reader.h:30
size_t max_sbn_jump
Maximum allowed source block number jump.
Definition: reader.h:32