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  //! Get SSRC element.
42  //! @pre Can be used if next() returned SSRC.
44 
45  //! Get REASON element.
46  //! Zero-terminated UTF-8 string.
47  //! String is valid only until next() call.
48  //! @pre Can be used if next() returned REASON.
49  const char* reason() const;
50 
51  private:
52  friend class ByeTraverser;
53 
54  explicit Iterator(const ByeTraverser& traverser);
55  void parse_ssrc_();
56  void parse_reason_();
57 
58  const ByeTraverser& traverser_;
59 
60  State state_;
62  uint8_t* pcur_;
63  size_t cur_ssrc_;
64 
65  packet::stream_source_t parsed_ssrc_;
66  char parsed_reason_[header::ByeReasonHeader::MaxTextLen + 1];
67  };
68 
69  //! Initialize traverser.
70  //! It will parse and iterate provided buffer.
71  explicit ByeTraverser(const core::Slice<uint8_t>& data);
72 
73  //! Parse packet from buffer.
74  bool parse();
75 
76  //! Construct iterator.
77  //! @pre Can be used if parse() returned true.
78  Iterator iter() const;
79 
80  //! Get number of SSRC elements in packet.
81  size_t ssrc_count() const;
82 
83 private:
84  const core::Slice<uint8_t> data_;
85  bool parsed_;
86  size_t packet_len_;
87  size_t ssrc_count_;
88 };
89 
90 } // namespace rtcp
91 } // namespace roc
92 
93 #endif // ROC_RTCP_BYE_TRAVERSER_H_
packet::stream_source_t ssrc() const
Get SSRC element.
const char * reason() const
Get REASON element. Zero-terminated UTF-8 string. String is valid only until next() call.
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 > &data)
Initialize traverser. It will parse and iterate provided buffer.
bool parse()
Parse packet from buffer.
static const size_t MaxTextLen
Get maximum allowed reason text length.
Definition: headers.h:891
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.