Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
bye_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/bye_traverser.h
10 //! @brief RTCP interface structures.
11 
12 #ifndef ROC_RTCP_BYE_TRAVERSER_H_
13 #define ROC_RTCP_BYE_TRAVERSER_H_
14 
15 #include "roc_core/slice.h"
16 #include "roc_core/stddefs.h"
17 #include "roc_core/time.h"
18 #include "roc_packet/units.h"
19 #include "roc_rtcp/headers.h"
20 
21 namespace roc {
22 namespace rtcp {
23 
24 //! BYE packet traverer.
25 class ByeTraverser {
26 public:
27  //! Packet iterator.
28  class Iterator {
29  public:
30  //! Iterator state.
31  enum State {
32  BEGIN, //!< Iterator created.
33  SSRC, //!< SSRC element.
34  REASON, //!< REASON element.
35  END //!< Parsed whole packet.
36  };
37 
38  //! Advance iterator.
40 
41  //! Check if there were any parsing errors.
42  bool error() const;
43 
44  //! Get SSRC element.
45  //! @pre Can be used if next() returned SSRC.
47 
48  //! Get REASON element.
49  //! Zero-terminated UTF-8 string.
50  //! Pointer is valid only until next() call.
51  //! @pre Can be used if next() returned REASON.
52  const char* get_reason() const;
53 
54  private:
55  friend class ByeTraverser;
56 
57  explicit Iterator(const ByeTraverser& traverser);
58  void next_element_();
59  void parse_ssrc_();
60  void parse_reason_();
61 
62  const ByeTraverser& traverser_;
63 
64  State state_;
65  const core::Slice<uint8_t> buf_;
66  size_t cur_pos_;
67  size_t cur_ssrc_;
68  bool error_;
69 
70  packet::stream_source_t parsed_ssrc_;
71  char parsed_reason_[header::MaxTextLen + 1];
72  };
73 
74  //! Initialize traverser.
75  //! It will parse and iterate provided buffer.
76  explicit ByeTraverser(const core::Slice<uint8_t>& buf);
77 
78  //! Parse packet from buffer.
79  bool parse();
80 
81  //! Construct iterator.
82  //! @pre Can be used if parse() returned true.
83  Iterator iter() const;
84 
85  //! Get number of SSRC elements in packet.
86  size_t ssrc_count() const;
87 
88 private:
90  bool parsed_;
91  size_t ssrc_count_;
92 };
93 
94 } // namespace rtcp
95 } // namespace roc
96 
97 #endif // ROC_RTCP_BYE_TRAVERSER_H_
packet::stream_source_t get_ssrc() const
Get SSRC element.
const char * get_reason() const
Get REASON element. Zero-terminated UTF-8 string. Pointer is valid only until next() call.
bool error() const
Check if there were any parsing errors.
State next()
Advance iterator.
BYE packet traverer.
Definition: bye_traverser.h:25
Iterator iter() const
Construct iterator.
size_t ssrc_count() const
Get number of SSRC elements in packet.
ByeTraverser(const core::Slice< uint8_t > &buf)
Initialize traverser. It will parse and iterate provided buffer.
bool parse()
Parse packet from buffer.
uint32_t stream_source_t
Packet stream identifier.
Definition: units.h:27
Root namespace.
RTCP headers.
Slice.
Commonly used types and functions.
Time definitions.
Various units used in packets.