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