-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrtcp.c
More file actions
268 lines (207 loc) · 8.74 KB
/
Copy pathrtcp.c
File metadata and controls
268 lines (207 loc) · 8.74 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/**
RTCP protocol
Copyright (C) 2016-2024 Michele Campus <michelecampus5@gmail.com>
This file is part of decoder.
[ The headers in this module follow the RFC 5246
and the pcap analyzed to have a real conformity
from theory and real traffic ]
decoder is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
decoder is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
decoder. If not, see <http://www.gnu.org/licenses/>.
**/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <byteswap.h>
#include "rtcp.h"
/**
Safely append formatted text to json_buffer at offset ret.
snprintf's return value can exceed the remaining buffer space when the
output is truncated, so the remaining size is clamped before use and the
return value is checked before it is added to ret. On truncation or
error the buffer position is capped and the function returns immediately.
**/
#define APPEND_JSON(...) \
do { \
size_t _rem = ((size_t)ret < (size_t)buffer_len) ? \
(size_t)buffer_len - (size_t)ret : 0; \
int _n = snprintf(json_buffer + ret, _rem, __VA_ARGS__); \
if (_n < 0) return -1; \
if ((size_t)_n >= _rem) { ret = buffer_len; return ret; } \
ret += _n; \
} while (0)
int check_rtcp_version (char *packet, int len) {
if(packet == NULL || len == 0) return -1;
rtcp_header_t *rtcp = (rtcp_header_t *)packet;
if(rtcp->version != 2)
{
fprintf(stderr, "wrong version\n");
return -2;
}
if(rtcp->type < RTCP_SR && rtcp->type > RTCP_XR) {
return -3;
}
return 1;
}
int rtcp_parser(char *packet, int len, char *json_buffer, int buffer_len) {
if(packet == NULL || len == 0) return -1;
rtcp_header_t *rtcp = (rtcp_header_t *)packet;
int ret = 0, is_xr = 0;
char *rptr;
// build the JSON buffer
APPEND_JSON("{ ");
int pno = 0, total = len;
while(rtcp) {
pno++;
switch(rtcp->type) {
case RTCP_SR: {
/* SR, sender report */
/* LDEBUG("#%d SR (200)\n", pno); */
printf("#%d SR (200)\n", pno);
rtcp_sr_t *sr = (rtcp_sr_t*)rtcp;
APPEND_JSON(SENDER_REPORT_JSON,
sender_info_get_ntp_timestamp_msw(&sr->si),
sender_info_get_ntp_timestamp_lsw(&sr->si),
sender_info_get_octet_count(&sr->si),
sender_info_get_rtp_timestamp(&sr->si),
sender_info_get_packet_count(&sr->si));
if(sr->header.rc > 0) {
APPEND_JSON(REPORT_BLOCK_JSON,
ntohl(sr->ssrc), rtcp->type,
report_block_get_ssrc(&sr->rb[0]),
report_block_get_high_ext_seq(&sr->rb[0]),
report_block_get_fraction_lost(&sr->rb[0]),
report_block_get_interarrival_jitter(&sr->rb[0]),
report_block_get_cum_packet_loss(&sr->rb[0]),
report_block_get_last_SR_time(&sr->rb[0]),
report_block_get_last_SR_delay(&sr->rb[0]));
}
break;
}
case RTCP_RR: {
/* RR, receiver report */
/* LDEBUG("#%d RR (201)\n", pno); */
printf("#%d RR (201)\n", pno);
rtcp_rr_t *rr = (rtcp_rr_t*)rtcp;
if(rr->header.rc > 0) {
APPEND_JSON(REPORT_BLOCK_JSON,
ntohl(rr->ssrc), rtcp->type,
report_block_get_ssrc(&rr->rb[0]),
report_block_get_high_ext_seq(&rr->rb[0]),
report_block_get_fraction_lost(&rr->rb[0]),
report_block_get_interarrival_jitter(&rr->rb[0]),
report_block_get_cum_packet_loss(&rr->rb[0]),
report_block_get_last_SR_time(&rr->rb[0]),
report_block_get_last_SR_delay(&rr->rb[0]));
}
break;
}
case RTCP_SDES: {
/* LDEBUG("#%d SDES (202)\n", pno); */
printf("#%d SDES (202)\n", pno);
/* if not needed send sdes */
/* if(!send_sdes) break; */
rtcp_sdes_t *sdes = (rtcp_sdes_t*)rtcp;
rptr = (char *)(rtcp + 2);
int sdes_report_count = 0;
char *end = (char*) rptr + (4*(rtcp_header_get_length(&sdes->header) + 1) -15);
APPEND_JSON(SDES_REPORT_BEGIN_JSON, ntohl(sdes->ssrc), sdes_chunk_get_csrc(&sdes->chunk));
while(rptr < end) {
if (rptr+2<=end) {
uint8_t chunk_type = rptr[0];
uint8_t chunk_len = rptr[1];
if(chunk_len == 0) break;
rptr += 2;
APPEND_JSON(SDES_REPORT_INFO_JSON, chunk_type, chunk_len, rptr);
sdes_report_count++;
if (rptr+chunk_len<=end) rptr+=chunk_len;
else break;
}
else {
break;
}
}
/* cut , off */
ret -= 1;
APPEND_JSON(SDES_REPORT_END_JSON, sdes_report_count);
break;
}
case RTCP_XR: {
/* LDEBUG("#%d XR (207)\n", pno); */
printf("#%d XR (207)\n", pno);
// set flag
is_xr = 1;
// cast the packet to rtcp-xr
struct rtcp_xr_t *rtcp_xr = (struct rtcp_xr_t *) rtcp;
// start to parse field and create json array
APPEND_JSON(EXTENDED_REPORT_JSON,
rtcpxr_header_get_type(&rtcp_xr->xr_header),
rtcpxr_header_get_id(&rtcp_xr->block),
rtcpxr_header_get_loss(&rtcp_xr->block),
rtcpxr_header_discard(&rtcp_xr->block),
rtcpxr_header_burst_rate(&rtcp_xr->block),
rtcpxr_header_gap_rate(&rtcp_xr->block),
rtcpxr_header_burst_duration(&rtcp_xr->block),
rtcpxr_header_gap_duration(&rtcp_xr->block),
rtcpxr_header_round_trip_del(&rtcp_xr->block),
rtcpxr_header_end_sys_delay(&rtcp_xr->block),
rtcpxr_header_signal_lev(&rtcp_xr->block),
rtcpxr_header_noise_lev(&rtcp_xr->block),
rtcpxr_header_RERL(&rtcp_xr->block),
rtcpxr_header_Gmin(&rtcp_xr->block),
rtcpxr_header_Rfact(&rtcp_xr->block),
rtcpxr_header_ext_Rfact(&rtcp_xr->block),
rtcpxr_header_MOS_LQ(&rtcp_xr->block),
rtcpxr_header_MOS_CQ(&rtcp_xr->block),
rtcpxr_header_PLC(&rtcp_xr->block),
rtcpxr_header_JB_adapt(&rtcp_xr->block),
rtcpxr_header_JB_rate(&rtcp_xr->block),
rtcpxr_header_JB_nom(&rtcp_xr->block),
rtcpxr_header_JB_max(&rtcp_xr->block),
rtcpxr_header_JB_abs_max(&rtcp_xr->block));
/* overwrite the trailing char with the closing brace */
if(ret > 0) ret -= 1;
APPEND_JSON("}");
break;
}
case RTCP_BYE: {
printf("\n#%d GOODBYE (203)\n", pno);
/* ret = 0; */
//rtcp_bye_t *bye = (rtcp_bye_t*)rtcp;
break;
}
case RTCP_APP: {
printf("\n#%d APP (204)\n", pno);
//rtcp_app_t *app = (rtcp_app_t*)rtcp;
break;
}
default:
break;
}
int length = ntohs(rtcp->length);
if(length == 0) {
break;
}
total -= length*4+4;
if(total <= 0) {
/* LDEBUG("End of RTCP packet\n"); */
printf("End of RTCP packet\n");
break;
}
rtcp = (rtcp_header_t *)((uint32_t*)rtcp + (length + 1));
}
/* bad parsed message */
if(ret < 10) return 0;
/* overwrite the trailing char with the closing brace */
if(ret > 0) ret -= 1;
APPEND_JSON("}");
if(is_xr == 1) printf("RTCP-XR packet\n");
return ret;
}