forked from maaron/rtp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtcp_packet.cpp
210 lines (171 loc) · 5.4 KB
/
rtcp_packet.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "stdafx.h"
#include <WinSock2.h>
#include "rtcp_packet.h"
#include "bits.h"
namespace media
{
rtcp_packet::rtcp_packet(void* data, size_t size)
: data_ptr((char*)data), header((uint32_t*)data),
write_ptr((char*)data),
end_ptr((char*)data + size)
{
}
void* rtcp_packet::data() { return data_ptr; }
void rtcp_packet::add_bytes(int bytes)
{
write_ptr += bytes;
uint16_t* length_ptr = (uint16_t*)header + 1;
*length_ptr = htons((packet_bytes_written() - sizeof(uint32_t)) / 4);
}
void rtcp_packet::increment_RC()
{
uint32_t h = ntohl(*header);
int rc = bf_get(h, 24, 5);
bf_set(h, rc + 1, 24, 5);
*header = htonl(h);
}
size_t rtcp_packet::compound_size()
{
return write_ptr - data_ptr;
}
size_t rtcp_packet::packet_size()
{
uint16_t* length = (uint16_t*)write_ptr + 1;
return (ntohs(*length) + 1) * sizeof(uint32_t);
}
void rtcp_packet::write32(uint32_t data)
{
*(uint32_t*)write_ptr = htonl(data);
add_bytes(sizeof(uint32_t));
}
uint32_t rtcp_packet::read32()
{
if (write_ptr >= end_ptr) return 0;
uint32_t value = htonl(*(uint32_t*)write_ptr);
write_ptr += sizeof(uint32_t);
return value;
}
void rtcp_packet::write_header(int pt)
{
header = (uint32_t*)write_ptr;
*header = htonl(0x80000000 | (pt << 16));
write_ptr += sizeof(uint32_t);
}
void rtcp_packet::write_sender_report(sender_report& sr)
{
write_header(RTCP_SR);
write32(sr.ssrc);
write32(sr.ntp_msw);
write32(sr.ntp_lsw);
write32(sr.rtp_timestamp);
write32(sr.octet_count);
write32(sr.packet_count);
}
void rtcp_packet::write_sender_report_block(report_block& block)
{
write32(block.ssrc);
write32((block.fraction_lost << 24) | (block.cumulative_lost & 0x00ffffff));
write32(block.extended_seq_received);
write32(block.interarrival_jitter);
write32(block.last_sr);
write32(block.delay_since_last_sr);
increment_RC();
}
void rtcp_packet::write_sdes(uint32_t src)
{
write_header(RTCP_SDES);
write32(src);
increment_RC();
}
void rtcp_packet::write_sdes_item(int id, const char* user_host)
{
unsigned char length = 0;
while (user_host[length++] != 0 && length != 255);
if (write_ptr + sizeof(sdes_item)+length >= end_ptr) return;
auto item = (sdes_item*)write_ptr;
item->type = id;
item->length = length;
memcpy(item + 1, user_host, length);
add_bytes(sizeof(sdes_item)+length);
}
void rtcp_packet::write_sdes_cname(const char* cname) { write_sdes_item(SDES_CNAME, cname); }
void rtcp_packet::write_sdes_name(const char* name) { write_sdes_item(SDES_NAME, name); }
void rtcp_packet::write_sdes_email(const char* email) { write_sdes_item(SDES_EMAIL, email); }
void rtcp_packet::write_sdes_end()
{
// Careful... this pad requires at least one byte
int num_pad_bytes = 4 - (packet_bytes_written() & 0x03);
if (write_ptr + num_pad_bytes >= end_ptr) return;
memset(write_ptr, 0, num_pad_bytes);
add_bytes(num_pad_bytes);
}
void rtcp_packet::write_bye(uint32_t src)
{
write_header(RTCP_BYE);
write32(src);
increment_RC();
}
void rtcp_packet::write_bye_src(uint32_t src)
{
write32(src);
increment_RC();
}
void rtcp_packet::write_bye_reason(const char* reason)
{
int length = 0;
while (reason[length++] != 0 && length < 256);
int num_pad_bytes = (4 - (packet_bytes_written() & 0x03)) & 0x03;
if (write_ptr + 1 + length + num_pad_bytes >= end_ptr) return;
*write_ptr = length;
memcpy(write_ptr + 1, reason, length);
memset(write_ptr + 1 + length, 0, num_pad_bytes);
add_bytes(1 + length + num_pad_bytes);
}
size_t rtcp_packet::packet_bytes_written()
{
return write_ptr - (char*)header;
}
bool rtcp_packet::move_next()
{
auto next_ptr = write_ptr + packet_size();
if (next_ptr >= end_ptr) return false;
else
{
write_ptr = next_ptr;
return true;
}
}
void rtcp_packet::read_header(rtcp_header& h)
{
uint32_t dword = read32();
h.V = bf_get(dword, 30, 2);
h.P = bf_get(dword, 29, 1);
h.RC = bf_get(dword, 24, 8);
h.PT = bf_get(dword, 16, 8);
h.length = bf_get(dword, 0, 16);
}
void rtcp_packet::read_sender_report(sender_report& sr)
{
sr.ssrc = read32();
sr.ntp_msw = read32();
sr.ntp_lsw = read32();
sr.rtp_timestamp = read32();
sr.packet_count = read32();
sr.octet_count = read32();
}
void rtcp_packet::read_report_block(report_block& block)
{
block.ssrc = read32();
uint32_t lost = read32();
block.fraction_lost = (lost >> 24) & 0x00ff;
block.cumulative_lost = lost & 0x00ffffff;
block.extended_seq_received = read32();
block.interarrival_jitter = read32();
block.last_sr = read32();
block.delay_since_last_sr = read32();
}
uint32_t rtcp_packet::read_ssrc()
{
return read32();
}
}