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 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/iallocator.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"
23 #include "roc_packet/packet_pool.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 allocator 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::PacketPool& packet_pool,
58  core::IAllocator& allocator);
59 
60  //! Check if object is successfully constructed.
61  bool valid() const;
62 
63  //! Did decoder catch block beginning?
64  bool started() const;
65 
66  //! Is decoder alive?
67  bool alive() const;
68 
69  //! Read packet.
70  //! @remarks
71  //! When a packet loss is detected, try to restore it from repair packets.
72  virtual packet::PacketPtr read();
73 
74 private:
75  packet::PacketPtr read_();
76 
77  packet::PacketPtr get_first_packet_();
78  packet::PacketPtr get_next_packet_();
79 
80  void next_block_();
81  void try_repair_();
82 
83  packet::PacketPtr parse_repaired_packet_(const core::Slice<uint8_t>& buffer);
84 
85  void fetch_packets_();
86 
87  void fill_block_();
88  void fill_source_block_();
89  void fill_repair_block_();
90 
91  bool process_source_packet_(const packet::PacketPtr&);
92  bool process_repair_packet_(const packet::PacketPtr&);
93 
94  bool validate_fec_packet_(const packet::PacketPtr&);
95  bool validate_sbn_sequence_(const packet::PacketPtr&);
96 
97  bool validate_incoming_source_packet_(const packet::PacketPtr&);
98  bool validate_incoming_repair_packet_(const packet::PacketPtr&);
99 
100  bool can_update_payload_size_(size_t);
101  bool can_update_source_block_size_(size_t);
102  bool can_update_repair_block_size_(size_t);
103 
104  bool update_payload_size_(size_t);
105  bool update_source_block_size_(size_t);
106  bool update_repair_block_size_(size_t);
107 
108  void drop_repair_packets_from_prev_blocks_();
109 
110  IBlockDecoder& decoder_;
111 
112  packet::IReader& source_reader_;
113  packet::IReader& repair_reader_;
114  packet::IParser& parser_;
115  packet::PacketPool& packet_pool_;
116 
117  packet::SortedQueue source_queue_;
118  packet::SortedQueue repair_queue_;
119 
120  core::Array<packet::PacketPtr> source_block_;
121  core::Array<packet::PacketPtr> repair_block_;
122 
123  bool valid_;
124 
125  bool alive_;
126  bool started_;
127  bool can_repair_;
128 
129  size_t next_packet_;
130  packet::blknum_t cur_sbn_;
131 
132  size_t payload_size_;
133 
134  bool source_block_resized_;
135  bool repair_block_resized_;
136  bool payload_resized_;
137 
138  unsigned n_packets_;
139 
140  const size_t max_sbn_jump_;
141  const packet::FECScheme fec_scheme_;
142 };
143 
144 } // namespace fec
145 } // namespace roc
146 
147 #endif // ROC_FEC_READER_H_
Memory allocator interface.
Definition: iallocator.h:23
FEC reader parameters.
Definition: reader.h:30
Packet reader interface.
Packet reader interface.
Definition: ireader.h:21
Packet pool.
Root namespace.
uint16_t blknum_t
FEC block number in a packet stream.
Definition: units.h:91
Sorted packet queue.
Dynamic array.
Packet parser interface.
Definition: iparser.h:22
Packet parser interface.
Base class for non-copyable objects.
Definition: noncopyable.h:23
size_t max_sbn_jump
Maximum allowed source block number jump.
Definition: reader.h:32
FEC block decoder interface.
FEC reader.
Definition: reader.h:40
Slice.
FECScheme
FECFRAME scheme.
Definition: fec.h:23
Packet.
Non-copyable object.
Memory allocator interface.
Sorted packet queue.
Definition: sorted_queue.h:27
FEC block decoder interface.