-
Notifications
You must be signed in to change notification settings - Fork 909
/
rms_sdp.c
401 lines (369 loc) · 10.4 KB
/
rms_sdp.c
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
* Copyright (C) 2017-2018 Julien Chavanton jchavanton@gmail.com
*
* This file is part of Kamailio, a free SIP server.
*
* Kamailio 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 2 of the License, or
* (at your option) any later version
*
* Kamailio 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "rms_sdp.h"
#include "rms_util.h"
#include "../../core/data_lump.h"
#include "../../core/parser/parse_content.h"
#include "../../core/parser/sdp/sdp.h"
// https://tools.ietf.org/html/rfc4566
// (protocol version)
const char *sdp_v = "v=0\r\n";
// (session name)
const char *sdp_s = "s=-\r\n";
// (time the session is active)
const char *sdp_t = "t=0 0\r\n";
//"a=rtpmap:101 telephone-event/8000\r\n"
//"a=fmtp:101 0-15\r\n";
//"a=rtpmap:0 PCMU/8000\r\n"
//"a=rtpmap:8 PCMA/8000\r\n"
//"a=rtpmap:96 opus/48000/2\r\n"
//"a=fmtp:96 useinbandfec=1\r\n";
int rms_get_sdp_info(rms_sdp_info_t *sdp_info, struct sip_msg *msg)
{
sdp_session_cell_t *sdp_session;
sdp_stream_cell_t *sdp_stream;
str media_ip, media_port;
int sdp_session_num = 0;
int sdp_stream_num = get_sdp_stream_num(msg);
if(parse_sdp(msg) < 0) {
LM_INFO("can not parse sdp\n");
return 0;
}
sdp_info_t *sdp = (sdp_info_t *)msg->body;
if(!sdp) {
LM_INFO("sdp null\n");
return 0;
}
rms_str_dup(&sdp_info->recv_body, &sdp->text, 1);
if(!sdp_info->recv_body.s)
goto error;
LM_INFO("sdp body - type[%d]\n", sdp->type);
if(sdp_stream_num > 1 || !sdp_stream_num) {
LM_INFO("only support one stream[%d]\n", sdp_stream_num);
}
sdp_stream_num = 0;
sdp_session = get_sdp_session(msg, sdp_session_num);
if(!sdp_session) {
return 0;
} else {
int sdp_stream_num = 0;
sdp_stream = get_sdp_stream(msg, sdp_session_num, sdp_stream_num);
if(!sdp_stream) {
LM_INFO("can not get the sdp stream\n");
return 0;
} else {
rms_str_dup(&sdp_info->payloads, &sdp_stream->payloads, 1);
if(!sdp_info->payloads.s)
goto error;
}
}
if(sdp_stream->ip_addr.s && sdp_stream->ip_addr.len > 0) {
media_ip = sdp_stream->ip_addr;
} else {
media_ip = sdp_session->ip_addr;
}
rms_str_dup(&sdp_info->remote_ip, &media_ip, 1);
if(!sdp_info->remote_ip.s)
goto error;
rms_str_dup(&media_port, &sdp_stream->port, 0);
if(!media_port.s)
goto error;
sdp_info->remote_port = atoi(media_port.s);
pkg_free(media_port.s);
return 1;
error:
rms_sdp_info_free(sdp_info);
return 0;
}
static char *rms_sdp_get_rtpmap(str body, int type_number)
{
char *pos = body.s;
while((pos = strstr(pos, "a=rtpmap:"))) {
int id;
int sampling_rate;
char codec[64];
sscanf(pos, "a=rtpmap:%d %s/%d", &id, codec, &sampling_rate);
if(id == type_number) {
LM_INFO("[%d][%s/%d]\n", id, codec, sampling_rate);
return rms_char_dup(codec, 1);
}
pos++;
}
return NULL;
}
void rms_sdp_info_init(rms_sdp_info_t *sdp_info)
{
memset(sdp_info, 0, sizeof(rms_sdp_info_t));
}
void rms_sdp_info_free(rms_sdp_info_t *sdp_info)
{
if(sdp_info->remote_ip.s) {
shm_free(sdp_info->remote_ip.s);
sdp_info->remote_ip.s = NULL;
}
if(sdp_info->payloads.s) {
shm_free(sdp_info->payloads.s);
sdp_info->payloads.s = NULL;
}
if(sdp_info->new_body.s) {
shm_free(sdp_info->new_body.s);
sdp_info->new_body.s = NULL;
}
}
int rms_sdp_prepare_new_body(rms_sdp_info_t *sdp_info, PayloadType *pt)
{
if(sdp_info->new_body.s)
return 0;
str *body = &sdp_info->new_body;
body->len = strlen(sdp_v) + strlen(sdp_s) + strlen(sdp_t);
// (originator and session identifier)
char sdp_o[128];
snprintf(
sdp_o, 128, "o=- 1028316687 1 IN IP4 %s\r\n", sdp_info->local_ip.s);
body->len += strlen(sdp_o);
// (connection information -- not required if included in all media)
char sdp_c[128];
snprintf(sdp_c, 128, "c=IN IP4 %s\r\n", sdp_info->local_ip.s);
body->len += strlen(sdp_c);
char sdp_m[128];
snprintf(sdp_m, 128, "m=audio %d RTP/AVP %d\r\n", sdp_info->udp_local_port,
pt->type);
body->len += strlen(sdp_m);
char sdp_a[128];
if(pt->type >= 96) {
if(strcasecmp(pt->mime_type, "opus") == 0) {
snprintf(sdp_a, 128, "a=rtpmap:%d opus/48000/2\r\n", pt->type);
} else {
snprintf(sdp_a, 128, "a=rtpmap:%d %s/%d/%d\r\n", pt->type,
pt->mime_type, pt->clock_rate, pt->channels);
}
body->len += strlen(sdp_a);
}
body->s = shm_malloc(body->len + 1);
if(!body->s)
return 0;
strcpy(body->s, sdp_v);
strcat(body->s, sdp_o);
strcat(body->s, sdp_s);
strcat(body->s, sdp_c);
strcat(body->s, sdp_t);
strcat(body->s, sdp_m);
if(pt->type >= 96) {
strcat(body->s, sdp_a);
}
return 1;
}
PayloadType *
rms_payload_type_new() // This could be problematic as it must remain compatible with the constructor in MS2.
{
PayloadType *newpayload = (PayloadType *)shm_malloc(sizeof(PayloadType));
if(!newpayload) {
return NULL;
}
memset(newpayload, 0, sizeof(PayloadType));
newpayload->flags |= PAYLOAD_TYPE_ALLOCATED;
return newpayload;
}
PayloadType *rms_sdp_check_payload_type(PayloadType *pt, rms_sdp_info_t *sdp)
{
pt->clock_rate = 8000;
pt->channels = 1;
pt->mime_type = NULL;
if(pt->type > 127) {
return NULL;
} else if(pt->type >= 96) {
char *rtpmap = rms_sdp_get_rtpmap(sdp->recv_body, pt->type);
if(!rtpmap)
return NULL;
char *s_mime_type = strtok(rtpmap, "/");
if(!s_mime_type) {
shm_free(rtpmap);
return NULL;
}
if(strcasecmp(s_mime_type, "opus") == 0) {
int payload_type = pt->type;
memcpy(pt, &payload_type_opus, sizeof(PayloadType));
pt->type = payload_type;
char *s_clock_rate = strtok(NULL, "/");
char *s_channels = strtok(NULL, "/");
if(s_clock_rate) {
pt->clock_rate = atoi(s_clock_rate);
} else {
pt->clock_rate = 8000;
}
if(s_channels) {
pt->channels = atoi(s_channels);
} else {
pt->channels = 2;
}
shm_free(rtpmap);
LM_INFO("[%p][%d][%s|%d|%d]\n", pt, pt->type, pt->mime_type,
pt->clock_rate, pt->channels);
return pt;
}
shm_free(pt->mime_type);
shm_free(rtpmap);
} else if(pt->type == 0) {
pt->mime_type = rms_char_dup("pcmu", 1); /* ia=rtpmap:0 PCMU/8000*/
} else if(pt->type == 8) {
pt->mime_type = rms_char_dup("pcma", 1);
}
// } else if (pt->type == 9) {
// pt->mime_type=rms_char_dup("g722", 1);
// } else if (pt->type == 18) {
// pt->mime_type=rms_char_dup("g729", 1);
// }
return pt;
}
PayloadType *rms_sdp_select_payload(rms_sdp_info_t *sdp)
{
// https://tools.ietf.org/html/rfc3551
LM_INFO("payloads[%s]\n", sdp->payloads.s); // 0 8
PayloadType *pt = rms_payload_type_new();
char *payloads = sdp->payloads.s;
char *payload_type_number = strtok(payloads, " ");
if(!pt) {
return NULL;
}
while(payload_type_number) {
pt->type = atoi(payload_type_number);
pt = rms_sdp_check_payload_type(pt, sdp);
if(pt->mime_type)
return pt;
payload_type_number = strtok(NULL, " ");
}
if(!pt->mime_type) {
LM_INFO("unsuported codec\n");
shm_free(pt); // payload_type_destroy(pt);
return NULL;
}
LM_INFO("payload_type:%d %s/%d/%d\n", pt->type, pt->mime_type,
pt->clock_rate, pt->channels);
return pt;
}
int rms_sdp_set_body(struct sip_msg *msg, str *new_body)
{
struct lump *anchor;
char *buf;
int len;
char *value_s;
int value_len;
str body = {0, 0};
str content_type_sdp = str_init("application/sdp");
if(!new_body->s || new_body->len == 0) {
LM_ERR("invalid body parameter\n");
return -1;
}
body.len = 0;
body.s = get_body(msg);
if(body.s == 0) {
LM_ERR("malformed sip message\n");
return -1;
}
del_nonshm_lump(&(msg->body_lumps));
msg->body_lumps = NULL;
if(msg->content_length) {
body.len = get_content_length(msg);
if(body.len > 0) {
if(body.s + body.len > msg->buf + msg->len) {
LM_ERR("invalid content length: %d\n", body.len);
return -1;
}
if(del_lump(msg, body.s - msg->buf, body.len, 0) == 0) {
LM_ERR("cannot delete existing body");
return -1;
}
}
}
anchor = anchor_lump(msg, msg->unparsed - msg->buf, 0, 0);
if(!anchor) {
LM_ERR("failed to get anchor\n");
return -1;
}
if(msg->content_length == 0) {
/* need to add Content-Length */
len = new_body->len;
value_s = int2str(len, &value_len);
LM_DBG("content-length: %d (%s)\n", value_len, value_s);
len = CONTENT_LENGTH_LEN + value_len + CRLF_LEN;
buf = pkg_malloc(sizeof(char) * (len));
if(!buf) {
LM_ERR("out of pkg memory\n");
return -1;
}
memcpy(buf, CONTENT_LENGTH, CONTENT_LENGTH_LEN);
memcpy(buf + CONTENT_LENGTH_LEN, value_s, value_len);
memcpy(buf + CONTENT_LENGTH_LEN + value_len, CRLF, CRLF_LEN);
if(insert_new_lump_after(anchor, buf, len, 0) == 0) {
LM_ERR("failed to insert content-length lump\n");
pkg_free(buf);
return -1;
}
}
/* add content-type */
if(msg->content_type == NULL
|| msg->content_type->body.len != content_type_sdp.len
|| strncmp(msg->content_type->body.s, content_type_sdp.s,
content_type_sdp.len)
!= 0) {
if(msg->content_type != NULL) {
if(del_lump(msg, msg->content_type->name.s - msg->buf,
msg->content_type->len, 0)
== 0) {
LM_ERR("failed to delete content type\n");
return -1;
}
}
value_len = content_type_sdp.len;
len = sizeof("Content-Type: ") - 1 + value_len + CRLF_LEN;
buf = pkg_malloc(sizeof(char) * (len));
if(!buf) {
LM_ERR("out of pkg memory\n");
return -1;
}
memcpy(buf, "Content-Type: ", sizeof("Content-Type: ") - 1);
memcpy(buf + sizeof("Content-Type: ") - 1, content_type_sdp.s,
value_len);
memcpy(buf + sizeof("Content-Type: ") - 1 + value_len, CRLF, CRLF_LEN);
if(insert_new_lump_after(anchor, buf, len, 0) == 0) {
LM_ERR("failed to insert content-type lump\n");
pkg_free(buf);
return -1;
}
}
anchor = anchor_lump(msg, body.s - msg->buf, 0, 0);
if(anchor == 0) {
LM_ERR("failed to get body anchor\n");
return -1;
}
buf = pkg_malloc(sizeof(char) * (new_body->len));
if(!buf) {
LM_ERR("out of pkg memory\n");
return -1;
}
memcpy(buf, new_body->s, new_body->len);
if(!insert_new_lump_after(anchor, buf, new_body->len, 0)) {
LM_ERR("failed to insert body lump\n");
pkg_free(buf);
return -1;
}
LM_DBG("new body: [%.*s]", new_body->len, new_body->s);
return 1;
}