Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
parser.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 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/parser.h
10 //! @brief FECFRAME packet parser.
11 
12 #ifndef ROC_FEC_PARSER_H_
13 #define ROC_FEC_PARSER_H_
14 
15 #include "roc_core/log.h"
16 #include "roc_core/noncopyable.h"
17 #include "roc_fec/headers.h"
18 #include "roc_packet/iparser.h"
19 
20 namespace roc {
21 namespace fec {
22 
23 //! FECFRAME packet parser.
24 template <class PayloadID, PayloadID_Type Type, PayloadID_Pos Pos>
25 class Parser : public packet::IParser, public core::NonCopyable<> {
26 public:
27  //! Initialization.
28  //! @remarks
29  //! Parses FECFRAME header or footer and passes the rest to @p inner_parser
30  //! if it's not null.
31  explicit Parser(packet::IParser* inner_parser)
32  : inner_parser_(inner_parser) {
33  }
34 
35  //! Parse packet from buffer.
36  virtual bool parse(packet::Packet& packet, const core::Slice<uint8_t>& buffer) {
37  if (buffer.size() < sizeof(PayloadID)) {
38  roc_log(LogDebug, "fec parser: bad packet, size < %d (payload id)",
39  (int)sizeof(PayloadID));
40  return false;
41  }
42 
43  const PayloadID* payload_id;
44  if (Pos == Header) {
45  payload_id = (const PayloadID*)buffer.data();
46  } else {
47  payload_id =
48  (const PayloadID*)(buffer.data() + buffer.size() - sizeof(PayloadID));
49  }
50 
51  if (Type == Repair) {
53  }
54 
56 
57  packet::FEC& fec = *packet.fec();
58 
59  fec.fec_scheme = PayloadID::fec_scheme();
60  fec.encoding_symbol_id = payload_id->esi();
61  fec.source_block_number = (packet::blknum_t)payload_id->sbn();
62  fec.source_block_length = payload_id->k();
63  fec.block_length = payload_id->n();
64 
65  if (Pos == Header) {
66  fec.payload = buffer.range(sizeof(PayloadID), buffer.size());
67  } else {
68  fec.payload = buffer.range(0, buffer.size() - sizeof(PayloadID));
69  }
70 
71  if (inner_parser_) {
72  return inner_parser_->parse(packet, fec.payload);
73  }
74 
75  return true;
76  }
77 
78 private:
79  packet::IParser* inner_parser_;
80 };
81 
82 } // namespace fec
83 } // namespace roc
84 
85 #endif // ROC_FEC_PARSER_H_
Packet contains repair FEC symbols.
Definition: packet.h:46
Slice range(size_t from, size_t to) const
Construct a slice pointing to a part of this slice.
Definition: slice.h:93
Packet contains FEC header.
Definition: packet.h:44
Source packet header of footer.
Definition: headers.h:27
Parser(packet::IParser *inner_parser)
Initialization.
Definition: parser.h:31
virtual bool parse(Packet &packet, const core::Slice< uint8_t > &buffer)=0
Parse packet from buffer.
Root namespace.
PayloadID comes before payload.
Definition: headers.h:32
FECFRAME packet parser.
Definition: parser.h:25
size_t size() const
Get number of elements in slice.
Definition: slice.h:69
uint16_t blknum_t
FEC block number in a packet stream.
Definition: units.h:91
const FEC * fec() const
FEC packet.
virtual bool parse(packet::Packet &packet, const core::Slice< uint8_t > &buffer)
Parse packet from buffer.
Definition: parser.h:36
FECFRAME packet.
Definition: fec.h:35
void add_flags(unsigned flags)
Add flags.
Packet parser interface.
Definition: iparser.h:22
Packet parser interface.
size_t source_block_length
Number of source packets in the block to which this packet belongs to.
Definition: fec.h:63
Base class for non-copyable objects.
Definition: noncopyable.h:23
Packet.
Definition: packet.h:35
T * data() const
Get slice data.
Definition: slice.h:61
#define roc_log(...)
Print message to log.
Definition: log.h:25
size_t encoding_symbol_id
The index number of packet in a block.
Definition: fec.h:49
FECScheme fec_scheme
The FEC scheme to which the packet belongs to.
Definition: fec.h:40
Debug message.
Definition: log.h:35
core::Slice< uint8_t > payload
FECFRAME payload.
Definition: fec.h:79
Non-copyable object.
FECFRAME headers.
Logging.
size_t block_length
Number of source packets and repair in the block to which this packet belongs to. ...
Definition: fec.h:71
blknum_t source_block_number
Number of a source block in a packet stream.
Definition: fec.h:57