Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
openfec_decoder.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/target_openfec/roc_fec/openfec_decoder.h
10 //! @brief Decoder implementation using OpenFEC library.
11 
12 #ifndef ROC_FEC_OPENFEC_DECODER_H_
13 #define ROC_FEC_OPENFEC_DECODER_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/codec_config.h"
20 #include "roc_fec/iblock_decoder.h"
22 #include "roc_packet/units.h"
23 
24 extern "C" {
25 #include <of_openfec_api.h>
26 }
27 
28 #ifndef OF_USE_DECODER
29 #error "OF_USE_DECODER undefined"
30 #endif
31 
32 #ifndef OF_USE_LDPC_STAIRCASE_CODEC
33 #error "OF_USE_LDPC_STAIRCASE_CODEC undefined"
34 #endif
35 
36 namespace roc {
37 namespace fec {
38 
39 //! Decoder implementation using OpenFEC library.
41 public:
42  //! Initialize.
43  explicit OpenfecDecoder(const CodecConfig& config,
44  packet::PacketFactory& packet_factory,
45  core::IArena& arena);
46 
47  virtual ~OpenfecDecoder();
48 
49  //! Check if object is successfully constructed.
50  bool is_valid() const;
51 
52  //! Get the maximum number of encoding symbols for the scheme being used.
53  virtual size_t max_block_length() const;
54 
55  //! Start block.
56  //!
57  //! @remarks
58  //! Performs an initial setup for a block. Should be called before
59  //! any operations for the block.
60  virtual bool begin(size_t sblen, size_t rblen, size_t payload_size);
61 
62  //! Store source or repair packet buffer for current block.
63  virtual void set(size_t index, const core::Slice<uint8_t>& buffer);
64 
65  //! Repair source packet buffer.
66  virtual core::Slice<uint8_t> repair(size_t index);
67 
68  //! Finish block.
69  //!
70  //! @remarks
71  //! Cleanups the resources allocated for the block. Should be called after
72  //! all operations for the block.
73  virtual void end();
74 
75 private:
76  void update_session_params_(size_t sblen, size_t rblen, size_t payload_size);
77 
78  void reset_tabs_();
79  bool resize_tabs_(size_t size);
80 
81  void update_();
82  void decode_();
83 
84  bool has_n_packets_(size_t n_packets) const;
85  bool is_optimal_() const;
86 
87  void reset_session_();
88  void destroy_session_();
89 
90  void report_();
91 
92  void fix_buffer_(size_t index);
93  void* make_buffer_(size_t index);
94 
95  static void* source_cb_(void* context, uint32_t size, uint32_t index);
96  static void* repair_cb_(void* context, uint32_t size, uint32_t index);
97 
98  size_t sblen_;
99  size_t rblen_;
100  size_t payload_size_;
101  size_t max_index_;
102 
103  of_codec_id_t codec_id_;
104  union {
105  of_rs_2_m_parameters_t rs_params_;
106  of_ldpc_parameters ldpc_params_;
107  } codec_params_;
108 
109  // session is recreated for every new block
110  of_session_t* of_sess_;
111  of_parameters_t* of_sess_params_;
112 
113  packet::PacketFactory& packet_factory_;
114 
115  // received and repaired source and repair packets
117 
118  // data of received and repaired source and repair packets
119  // points to buff_tab_[x].data() or to memory allocated by OpenFEC
120  core::Array<void*> data_tab_;
121 
122  // true if packet is received, false if it's is lost or repaired
123  core::Array<bool> recv_tab_;
124 
125  // for debug logging
126  core::Array<char> status_;
127 
128  bool has_new_packets_;
129  bool decoding_finished_;
130 
131  size_t max_block_length_;
132 
133  bool valid_;
134 };
135 
136 } // namespace fec
137 } // namespace roc
138 
139 #endif // ROC_FEC_OPENFEC_DECODER_H_
Dynamic array.
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.
Decoder implementation using OpenFEC library.
virtual void end()
Finish block.
virtual bool begin(size_t sblen, size_t rblen, size_t payload_size)
Start block.
virtual core::Slice< uint8_t > repair(size_t index)
Repair source packet buffer.
virtual void set(size_t index, const core::Slice< uint8_t > &buffer)
Store source or repair packet buffer for current block.
bool is_valid() const
Check if object is successfully constructed.
virtual size_t max_block_length() const
Get the maximum number of encoding symbols for the scheme being used.
OpenfecDecoder(const CodecConfig &config, packet::PacketFactory &packet_factory, core::IArena &arena)
Initialize.
FEC codec parameters.
Memory arena interface.
FEC block decoder interface.
Root namespace.
Non-copyable object.
Packet factory.
Slice.
FEC codec parameters.
Definition: codec_config.h:22
Various units used in packets.