Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
parser.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017 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/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"
17#include "roc_fec/headers.h"
18#include "roc_packet/iparser.h"
19
20namespace roc {
21namespace fec {
22
23//! FECFRAME packet parser.
24template <class PayloadID, PayloadID_Type Type, PayloadID_Pos Pos>
25class Parser : public packet::IParser, public core::NonCopyable<> {
26public:
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.subslice(sizeof(PayloadID), buffer.size());
67 } else {
68 fec.payload = buffer.subslice(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
78private:
79 packet::IParser* inner_parser_;
80};
81
82} // namespace fec
83} // namespace roc
84
85#endif // ROC_FEC_PARSER_H_
Base class for non-copyable objects.
Definition noncopyable.h:23
Shared ownership intrusive pointer.
Definition shared_ptr.h:32
FECFRAME packet parser.
Definition parser.h:25
virtual bool parse(packet::Packet &packet, const core::Slice< uint8_t > &buffer)
Parse packet from buffer.
Definition parser.h:36
Parser(packet::IParser *inner_parser)
Initialization.
Definition parser.h:31
Packet parser interface.
Definition iparser.h:22
virtual bool parse(Packet &packet, const core::Slice< uint8_t > &buffer)=0
Parse packet from buffer.
const FEC * fec() const
FEC packet.
@ FlagRepair
Packet contains repair FEC symbols.
Definition packet.h:51
@ FlagFEC
Packet contains FEC header.
Definition packet.h:48
void add_flags(unsigned flags)
Add flags.
Packet parser interface.
Logging.
#define roc_log(level,...)
Print message to log.
Definition log.h:31
@ Header
PayloadID comes before payload.
Definition headers.h:32
@ Repair
Source packet header of footer.
Definition headers.h:27
uint16_t blknum_t
FEC packet block number.
Definition units.h:130
Root namespace.
@ LogDebug
Regular debug message.
Definition log.h:48
Non-copyable object.
FECFRAME headers.
FECFRAME packet.
Definition fec.h:35
core::Slice< uint8_t > payload
FECFRAME payload.
Definition fec.h:74
size_t encoding_symbol_id
The index number of packet in a block ("esi").
Definition fec.h:47
size_t source_block_length
Number of source packets in block to which this packet belongs ("sblen").
Definition fec.h:59
FecScheme fec_scheme
The FEC scheme to which the packet belongs to.
Definition fec.h:39
size_t block_length
Number of source + repair packets in block to which this packet belongs ("blen").
Definition fec.h:66
blknum_t source_block_number
Number of a source block in a packet stream ("sbn").
Definition fec.h:54