libflute
Public Member Functions | Static Public Member Functions | List of all members
LibFlute::EncodingSymbol Class Reference

A class for handling FEC encoding symbols. More...

#include <EncodingSymbol.h>

Public Member Functions

 EncodingSymbol (uint32_t id, uint32_t source_block_number, char *encoded_data, size_t data_len, FecScheme fec_scheme)
 Default constructor. More...
 
virtual ~EncodingSymbol ()
 Default destructor. More...
 
uint32_t id () const
 Get the encoding symbol ID. More...
 
uint32_t source_block_number () const
 Get the source block number. More...
 
void decode_to (char *buffer, size_t max_length) const
 Decode to a buffer. More...
 
size_t encode_to (char *buffer, size_t max_length) const
 Encode to a buffer. More...
 
size_t len () const
 Get the data length. More...
 

Static Public Member Functions

static std::vector< EncodingSymbolfrom_payload (char *encoded_data, size_t data_len, const FecOti &fec_oti, ContentEncoding encoding)
 Parse and construct all encoding symbols from a payload data buffer. More...
 
static size_t to_payload (const std::vector< EncodingSymbol > &, char *encoded_data, size_t data_len, const FecOti &fec_oti, ContentEncoding encoding)
 Write encoding symbols to a packet payload buffer. More...
 

Detailed Description

A class for handling FEC encoding symbols.

Definition at line 26 of file EncodingSymbol.h.

Constructor & Destructor Documentation

◆ EncodingSymbol()

LibFlute::EncodingSymbol::EncodingSymbol ( uint32_t  id,
uint32_t  source_block_number,
char *  encoded_data,
size_t  data_len,
FecScheme  fec_scheme 
)
inline

Default constructor.

Parameters
idEncoding symbol ID
source_block_numberSource BLock Number of the symbol
encoded_dataEncoded content data
data_lenLength of the encoded data
fec_schemeFEC scheme to use

Definition at line 47 of file EncodingSymbol.h.

48  : _id(id)
49  , _source_block_number(source_block_number)
50  , _fec_scheme(fec_scheme)
51  , _encoded_data(encoded_data)
52  , _data_len(data_len)
53  {};
uint32_t source_block_number() const
Get the source block number.

◆ ~EncodingSymbol()

virtual LibFlute::EncodingSymbol::~EncodingSymbol ( )
inlinevirtual

Default destructor.

Definition at line 58 of file EncodingSymbol.h.

58 {};

Member Function Documentation

◆ decode_to()

auto LibFlute::EncodingSymbol::decode_to ( char *  buffer,
size_t  max_length 
) const

Decode to a buffer.

Definition at line 87 of file EncodingSymbol.cpp.

87  {
88  if (_fec_scheme == FecScheme::CompactNoCode) {
89  if (_data_len <= max_length) {
90  memcpy(buffer, _encoded_data, _data_len);
91  }
92  }
93 }

◆ encode_to()

auto LibFlute::EncodingSymbol::encode_to ( char *  buffer,
size_t  max_length 
) const

Encode to a buffer.

Definition at line 95 of file EncodingSymbol.cpp.

95  {
96  if (_fec_scheme == FecScheme::CompactNoCode) {
97  if (_data_len <= max_length) {
98  memcpy(buffer, _encoded_data, _data_len);
99  return _data_len;
100  }
101  }
102  return 0;
103 }

◆ from_payload()

auto LibFlute::EncodingSymbol::from_payload ( char *  encoded_data,
size_t  data_len,
const FecOti fec_oti,
ContentEncoding  encoding 
)
static

Parse and construct all encoding symbols from a payload data buffer.

Definition at line 25 of file EncodingSymbol.cpp.

26 {
27  auto source_block_number = 0;
28  auto encoding_symbol_id = 0;
29  std::vector<EncodingSymbol> symbols;
30 
31  if (encoding != ContentEncoding::NONE && encoding != ContentEncoding::GZIP) {
32  throw std::runtime_error("Only unencoded or gzipped content is supported");
33  }
34 
35  if (fec_oti.encoding_id == FecScheme::CompactNoCode) {
36  source_block_number = ntohs(*(uint16_t*)encoded_data);
37  encoded_data += 2;
38  encoding_symbol_id = ntohs(*(uint16_t*)encoded_data);
39  encoded_data += 2;
40  data_len -= 4;
41  } else {
42  throw std::runtime_error("Only compact no-code FEC is supported");
43  }
44 
45  int nof_symbols = std::ceil((float)data_len / (float)fec_oti.encoding_symbol_length);
46  for (int i = 0; i < nof_symbols; i++) {
47  if (fec_oti.encoding_id == FecScheme::CompactNoCode) {
48  symbols.emplace_back(encoding_symbol_id, source_block_number, encoded_data, std::min(data_len, (size_t)fec_oti.encoding_symbol_length), fec_oti.encoding_id);
49  }
50  encoded_data += fec_oti.encoding_symbol_length;
51  encoding_symbol_id++;
52  }
53 
54  return symbols;
55 }

◆ id()

uint32_t LibFlute::EncodingSymbol::id ( ) const
inline

Get the encoding symbol ID.

Definition at line 63 of file EncodingSymbol.h.

63 { return _id; };

◆ len()

size_t LibFlute::EncodingSymbol::len ( ) const
inline

Get the data length.

Definition at line 83 of file EncodingSymbol.h.

83 { return _data_len; };

◆ source_block_number()

uint32_t LibFlute::EncodingSymbol::source_block_number ( ) const
inline

Get the source block number.

Definition at line 68 of file EncodingSymbol.h.

68 { return _source_block_number; };

◆ to_payload()

auto LibFlute::EncodingSymbol::to_payload ( const std::vector< EncodingSymbol > &  symbols,
char *  encoded_data,
size_t  data_len,
const FecOti fec_oti,
ContentEncoding  encoding 
)
static

Write encoding symbols to a packet payload buffer.

Definition at line 57 of file EncodingSymbol.cpp.

58 {
59  size_t len = 0;
60  auto ptr = encoded_data;
61  auto first_symbol = symbols.begin();
62  if (fec_oti.encoding_id == FecScheme::CompactNoCode && data_len >= 4) {
63  *((uint16_t*)ptr) = htons(first_symbol->source_block_number());
64  ptr += 2;
65  *((uint16_t*)ptr) = htons(first_symbol->id());
66  ptr += 2;
67  len += 4;
68  data_len -= 4;
69  } else {
70  throw std::runtime_error("Only compact no-code FEC is supported");
71  }
72 
73  for (const auto& symbol : symbols) {
74  if (symbol.len() <= data_len) {
75  auto symbol_len = symbol.encode_to(ptr, data_len);
76  data_len -= symbol_len;
77  len += symbol_len;
78  ptr += symbol_len;
79  } else {
80  spdlog::warn("Not enough space in payload buffer for encoding symbol");
81  break;
82  }
83  }
84  return len;
85 }
size_t len() const
Get the data length.

The documentation for this class was generated from the following files: