libflute
EncodingSymbol.cpp
Go to the documentation of this file.
1 // libflute - FLUTE/ALC library
2 //
3 // Copyright (C) 2021 Klaus Kühnhammer (Österreichische Rundfunksender GmbH & Co KG)
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Affero General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Affero General Public License for more details.
14 //
15 // You should have received a copy of the GNU Affero General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 //
18 #include <cstdio>
19 #include <cstring>
20 #include <iostream>
21 #include <cmath>
22 #include <arpa/inet.h>
23 #include "EncodingSymbol.h"
24 
25 auto LibFlute::EncodingSymbol::from_payload(char* encoded_data, size_t data_len, const FecOti& fec_oti, ContentEncoding encoding) -> std::vector<EncodingSymbol>
26 {
27  auto source_block_number = 0;
28  auto encoding_symbol_id = 0;
29  std::vector<EncodingSymbol> symbols;
30 
31  if (encoding != ContentEncoding::NONE) {
32  throw "Only unencoded 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 "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 }
56 
57 auto LibFlute::EncodingSymbol::to_payload(const std::vector<EncodingSymbol>& symbols, char* encoded_data, size_t data_len, const FecOti& fec_oti, ContentEncoding encoding) -> size_t
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) {
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  } else {
69  throw "Only compact no-code FEC is supported";
70  }
71 
72  for (const auto& symbol : symbols) {
73  if (symbol.len() <= data_len) {
74  auto symbol_len = symbol.encode_to(ptr, data_len);
75  data_len -= symbol_len;
76  encoded_data += symbol_len;
77  len += symbol_len;
78  }
79  }
80  return len;
81 }
82 
83 auto LibFlute::EncodingSymbol::decode_to(char* buffer, size_t max_length) const -> void {
84  if (_fec_scheme == FecScheme::CompactNoCode) {
85  if (_data_len <= max_length) {
86  memcpy(buffer, _encoded_data, _data_len);
87  }
88  }
89 }
90 
91 auto LibFlute::EncodingSymbol::encode_to(char* buffer, size_t max_length) const -> size_t {
92  if (_fec_scheme == FecScheme::CompactNoCode) {
93  if (_data_len <= max_length) {
94  memcpy(buffer, _encoded_data, _data_len);
95  return _data_len;
96  }
97  }
98  return 0;
99 }
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.
size_t encode_to(char *buffer, size_t max_length) const
Encode to a buffer.
static std::vector< EncodingSymbol > from_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.
void decode_to(char *buffer, size_t max_length) const
Decode to a buffer.
ContentEncoding
Content Encodings.
Definition: flute_types.h:36
OTI values struct.
Definition: flute_types.h:53