Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
headers.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 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_rtp/headers.h
10 //! @brief RTP headers.
11 
12 #ifndef ROC_RTP_HEADERS_H_
13 #define ROC_RTP_HEADERS_H_
14 
15 #include "roc_core/attributes.h"
16 #include "roc_core/endian.h"
17 #include "roc_core/panic.h"
18 #include "roc_core/stddefs.h"
19 
20 namespace roc {
21 namespace rtp {
22 
23 //! RTP protocol version.
24 enum Version {
25  V2 = 2 //!< RTP version 2.
26 };
27 
28 //! RTP payload type.
30  PayloadType_L16_Stereo = 10, //!< Audio, 16-bit samples, 2 channels, 44100 Hz.
31  PayloadType_L16_Mono = 11 //!< Audio, 16-bit samples, 1 channel, 44100 Hz.
32 };
33 
34 //! RTP header.
35 //! @remarks
36 //! Contains fixed size part of 12 bytes and variable size CSRC array.
37 //!
38 //! @code
39 //! 0 1 2 3 4
40 //! 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
41 //! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 //! |V=2|P|X| CC |M| PT | sequence number |
43 //! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 //! | timestamp |
45 //! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 //! | SSRC |
47 //! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48 //! | CSRC |
49 //! | .... |
50 //! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51 //! @endcode
53 private:
54  enum {
55  //! @name RTP protocol version.
56  // @{
57  Flag_VersionShift = 6,
58  Flag_VersionMask = 0x3,
59  // @}
60 
61  //! @name RTP padding flag.
62  //! @remarks
63  //! If this flag is set, packet contains padding at the end. Last byte
64  //! contains padding length.
65  // @{
66  Flag_PaddingShift = 5,
67  Flag_PaddingMask = 0x1,
68  // @}
69 
70  //! @name RTP extension header flag.
71  //! @remarks
72  //! If this flag is set, packet contains extension header between main
73  //! header and payload.
74  // @{
75  Flag_ExtensionShift = 4,
76  Flag_ExtensionMask = 0x1,
77  // @}
78 
79  //! @name Number of CSRC items at the end of RTP header.
80  // @{
81  Flag_CSRCShift = 0,
82  Flag_CSRCMask = 0xf,
83  // @}
84 
85  //! @name RTP marker bit.
86  //! @remarks
87  //! Semantics of marker bit may vary and is defined by profile in use.
88  // @{
89  MPT_MarkerShift = 7,
90  MPT_MarkerMask = 0x1,
91  // @}
92 
93  //! @name RTP payload type.
94  // @{
95  MPT_PayloadTypeShift = 0,
96  MPT_PayloadTypeMask = 0x7f
97  // @}
98  };
99 
100  //! Packed flags (Flag_*).
101  uint8_t flags_;
102 
103  //! Packed marker and payload type fields (MPT__*).
104  uint8_t mpt_;
105 
106  //! Sequence number.
107  uint16_t seqnum_;
108 
109  //! Timestamp.
110  uint32_t timestamp_;
111 
112  //! Stream identifiers (SSRC and zero or more CSRC).
113  uint32_t ssrc_[1];
114 
115 public:
116  //! Get header size in bytes.
117  uint32_t header_size() const {
118  roc_panic_if(sizeof(*this) != 12);
119  return sizeof(*this) + num_csrc() * sizeof(uint32_t);
120  }
121 
122  //! Clear header.
123  void clear() {
124  memset(this, 0, sizeof(*this));
125  }
126 
127  //! Get version.
128  uint8_t version() const {
129  return ((flags_ >> Flag_VersionShift) & Flag_VersionMask);
130  }
131 
132  //! Set version.
134  roc_panic_if((v & Flag_VersionMask) != v);
135  flags_ &= ~(Flag_VersionMask << Flag_VersionShift);
136  flags_ |= (v << Flag_VersionShift);
137  }
138 
139  //! Get padding flag.
140  bool has_padding() const {
141  return (flags_ & (Flag_PaddingMask << Flag_PaddingShift));
142  }
143 
144  //! Set padding flag.
145  void set_padding(bool v) {
146  flags_ &= ~(Flag_PaddingMask << Flag_PaddingShift);
147  flags_ |= ((v ? 1 : 0) << Flag_PaddingShift);
148  }
149 
150  //! Get extension flag.
151  bool has_extension() const {
152  return (flags_ & (Flag_ExtensionMask << Flag_ExtensionShift));
153  }
154 
155  //! Get CSRC array size.
156  uint8_t num_csrc() const {
157  return ((flags_ >> Flag_CSRCShift) & Flag_CSRCMask);
158  }
159 
160  //! Get payload type.
161  uint8_t payload_type() const {
162  return ((mpt_ >> MPT_PayloadTypeShift) & MPT_PayloadTypeMask);
163  }
164 
165  //! Set payload type.
166  void set_payload_type(uint8_t pt) {
167  roc_panic_if((pt & MPT_PayloadTypeMask) != pt);
168  mpt_ &= ~(MPT_PayloadTypeMask << MPT_PayloadTypeShift);
169  mpt_ |= (pt << MPT_PayloadTypeShift);
170  }
171 
172  //! Get marker bit.
173  bool marker() const {
174  return (mpt_ & (MPT_MarkerMask << MPT_MarkerShift));
175  }
176 
177  //! Set marker bit.
178  void set_marker(bool m) {
179  mpt_ &= ~(MPT_MarkerMask << MPT_MarkerShift);
180  mpt_ |= ((!!m) << MPT_MarkerShift);
181  }
182 
183  //! Get sequence number.
184  uint16_t seqnum() const {
185  return core::ntoh16(seqnum_);
186  }
187 
188  //! Set sequence number.
189  void set_seqnum(uint16_t sn) {
190  seqnum_ = core::hton16(sn);
191  }
192 
193  //! Get timestamp.
194  uint32_t timestamp() const {
195  return core::ntoh32(timestamp_);
196  }
197 
198  //! Set timestamp.
199  void set_timestamp(uint32_t ts) {
200  timestamp_ = core::hton32(ts);
201  }
202 
203  //! Get SSRC.
204  uint32_t ssrc() const {
205  return core::ntoh32(ssrc_[0]);
206  }
207 
208  //! Set SSRC.
209  void set_ssrc(uint32_t s) {
210  ssrc_[0] = core::hton32(s);
211  }
212 
213  //! Get CSRC.
214  uint32_t csrc(size_t index) const {
215  roc_panic_if(index >= num_csrc());
216  return core::ntoh32(ssrc_[index + 1]);
217  }
218 };
219 
220 //! RTP extension header.
221 //! @remarks
222 //! Extension contains fixed size header of 4 bytes followed by variable
223 //! length data.
224 //!
225 //! @code
226 //! 0 1 2 3 4
227 //! 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
228 //! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
229 //! | type | length |
230 //! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
231 //! | extension data |
232 //! | .... |
233 //! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
234 //! @endcode
236 private:
237  //! Extenson type.
238  uint16_t type_;
239 
240  //! Number of 32-bit words in data following extension header.
241  uint16_t len_;
242 
243 public:
244  //! Get extension type.
245  uint16_t type() const {
246  return core::ntoh16(type_);
247  }
248 
249  //! Get extension data size in bytes (without extension header itself).
250  uint32_t data_size() const {
251  return (uint32_t(core::ntoh16(len_)) << 2);
252  }
253 };
254 
255 } // namespace rtp
256 } // namespace roc
257 
258 #endif // ROC_RTP_HEADERS_H_
RTP header.
Definition: headers.h:52
uint16_t ntoh16(uint16_t v)
Network to host (16 bits).
Definition: endian.h:24
void clear()
Clear header.
Definition: headers.h:123
RTP extension header.
Definition: headers.h:235
#define roc_panic_if(x)
Panic if condition is true.
Definition: panic.h:26
uint8_t num_csrc() const
Get CSRC array size.
Definition: headers.h:156
GCC attributes.
void set_payload_type(uint8_t pt)
Set payload type.
Definition: headers.h:166
RTP version 2.
Definition: headers.h:25
uint8_t payload_type() const
Get payload type.
Definition: headers.h:161
#define ROC_ATTR_PACKED
Structure&#39;s fields are packed.
Definition: attributes.h:25
Root namespace.
uint32_t csrc(size_t index) const
Get CSRC.
Definition: headers.h:214
uint32_t ntoh32(uint32_t v)
Network to host (32 bits).
Definition: endian.h:29
void set_marker(bool m)
Set marker bit.
Definition: headers.h:178
uint16_t type() const
Get extension type.
Definition: headers.h:245
void set_version(Version v)
Set version.
Definition: headers.h:133
void set_seqnum(uint16_t sn)
Set sequence number.
Definition: headers.h:189
uint32_t ssrc() const
Get SSRC.
Definition: headers.h:204
uint32_t timestamp() const
Get timestamp.
Definition: headers.h:194
Endian converters.
Commonly used types and functions.
Audio, 16-bit samples, 1 channel, 44100 Hz.
Definition: headers.h:31
uint32_t hton32(uint32_t v)
Host to network (32 bits).
Definition: endian.h:39
void set_ssrc(uint32_t s)
Set SSRC.
Definition: headers.h:209
Version
RTP protocol version.
Definition: headers.h:24
bool has_padding() const
Get padding flag.
Definition: headers.h:140
Panic function.
PayloadType
RTP payload type.
Definition: headers.h:29
bool has_extension() const
Get extension flag.
Definition: headers.h:151
uint32_t data_size() const
Get extension data size in bytes (without extension header itself).
Definition: headers.h:250
bool marker() const
Get marker bit.
Definition: headers.h:173
void set_padding(bool v)
Set padding flag.
Definition: headers.h:145
Audio, 16-bit samples, 2 channels, 44100 Hz.
Definition: headers.h:30
uint8_t version() const
Get version.
Definition: headers.h:128
void set_timestamp(uint32_t ts)
Set timestamp.
Definition: headers.h:199
uint16_t seqnum() const
Get sequence number.
Definition: headers.h:184
uint32_t header_size() const
Get header size in bytes.
Definition: headers.h:117
uint16_t hton16(uint16_t v)
Host to network (16 bits).
Definition: endian.h:34