Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
traverser.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022 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_rtcp/traverser.h
10 //! @brief RTCP packet traverser.
11 
12 #ifndef ROC_RTCP_TRAVERSER_H_
13 #define ROC_RTCP_TRAVERSER_H_
14 
15 #include "roc_rtcp/bye_traverser.h"
16 #include "roc_rtcp/headers.h"
18 #include "roc_rtcp/xr_traverser.h"
19 
20 namespace roc {
21 namespace rtcp {
22 
23 //! RTCP compound packet traverser.
24 class Traverser {
25 public:
26  //! Packet iterator.
27  class Iterator {
28  public:
29  //! Iterator state.
30  enum State {
31  BEGIN, //!< Iterator created.
32  SR, //!< SR packet.
33  RR, //!< RR packet.
34  XR, //!< XR packet.
35  SDES, //!< SDES packet.
36  BYE, //!< BYE packet.
37  END //!< Parsed whole compound packet.
38  };
39 
40  //! Advance iterator.
42 
43  //! Check if there were any parsing errors.
44  bool error() const;
45 
46  //! Get SR packet.
47  //! @pre Can be used if next() returned SR.
49 
50  //! Get RR packet.
51  //! @pre Can be used if next() returned RR.
53 
54  //! Get traverser for XR packet.
55  //! @pre Can be used if next() returned XR.
57 
58  //! Get traverser for SDES packet.
59  //! @pre Can be used if next() returned SDES.
61 
62  //! Get traverser for BYE packet.
63  //! @pre Can be used if next() returned BYE.
65 
66  private:
67  friend class Traverser;
68 
69  explicit Iterator(const Traverser& traverser);
70  void next_packet_();
71  bool remove_padding_();
72  bool check_sr_();
73  bool check_rr_();
74 
75  State state_;
76  const core::Slice<uint8_t> buf_;
77  size_t cur_pos_;
78  const header::PacketHeader* cur_pkt_header_;
79  size_t cur_pkt_len_;
80  core::Slice<uint8_t> cur_pkt_slice_;
81  bool error_;
82  };
83 
84  //! Initialize traverser.
85  //! It will parse and iterate provided buffer.
86  explicit Traverser(const core::Slice<uint8_t>& buf);
87 
88  //! Parse packet from buffer.
89  bool parse();
90 
91  //! Construct iterator.
92  //! @pre Can be used if parse() returned true.
93  Iterator iter() const;
94 
95 private:
96  const core::Slice<uint8_t> buf_;
97  bool parsed_;
98 };
99 
100 } // namespace rtcp
101 } // namespace roc
102 
103 #endif // ROC_RTCP_TRAVERSER_H_
RTCP interface structures.
BYE packet traverer.
Definition: bye_traverser.h:25
SDES packet traverer.
State next()
Advance iterator.
const header::SenderReportPacket & get_sr() const
Get SR packet.
const header::ReceiverReportPacket & get_rr() const
Get RR packet.
bool error() const
Check if there were any parsing errors.
SdesTraverser get_sdes()
Get traverser for SDES packet.
ByeTraverser get_bye()
Get traverser for BYE packet.
XrTraverser get_xr() const
Get traverser for XR packet.
@ BEGIN
Iterator created.
Definition: traverser.h:31
@ END
Parsed whole compound packet.
Definition: traverser.h:37
RTCP compound packet traverser.
Definition: traverser.h:24
Traverser(const core::Slice< uint8_t > &buf)
Initialize traverser. It will parse and iterate provided buffer.
Iterator iter() const
Construct iterator.
bool parse()
Parse packet from buffer.
XR packet traverser.
Definition: xr_traverser.h:23
RTCP packet header, common for all RTCP packet types.
Definition: headers.h:126
Receiver Report RTCP packet (RR).
Definition: headers.h:524
Sender Report RTCP packet (SR).
Definition: headers.h:621
Root namespace.
RTCP headers.
Sdes enums.
XR Traverser.