forked from meetecho/janus-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtp.c
603 lines (575 loc) · 20.9 KB
/
rtp.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
/*! \file rtp.c
* \author Lorenzo Miniero <[email protected]>
* \copyright GNU General Public License v3
* \brief RTP processing
* \details Implementation of the RTP header. Since the gateway does not
* much more than relaying frames around, the only thing we're interested
* in is the RTP header and how to get its payload, and parsing extensions.
*
* \ingroup protocols
* \ref protocols
*/
#include <string.h>
#include "rtp.h"
#include "rtpsrtp.h"
#include "debug.h"
#include "utils.h"
char *janus_rtp_payload(char *buf, int len, int *plen) {
if(!buf || len < 12)
return NULL;
janus_rtp_header *rtp = (janus_rtp_header *)buf;
int hlen = 12;
if(rtp->csrccount) /* Skip CSRC if needed */
hlen += rtp->csrccount*4;
if(rtp->extension) {
janus_rtp_header_extension *ext = (janus_rtp_header_extension*)(buf+hlen);
int extlen = ntohs(ext->length)*4;
hlen += 4;
if(len > (hlen + extlen))
hlen += extlen;
}
if(plen)
*plen = len-hlen;
return buf+hlen;
}
int janus_rtp_header_extension_get_id(const char *sdp, const char *extension) {
if(!sdp || !extension)
return -1;
char extmap[100];
g_snprintf(extmap, 100, "a=extmap:%%d %s", extension);
/* Look for the extmap */
const char *line = strstr(sdp, "m=");
while(line) {
char *next = strchr(line, '\n');
if(next) {
*next = '\0';
if(strstr(line, "a=extmap") && strstr(line, extension)) {
/* Gotcha! */
int id = 0;
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
if(sscanf(line, extmap, &id) == 1) {
#pragma GCC diagnostic warning "-Wformat-nonliteral"
*next = '\n';
return id;
}
}
*next = '\n';
}
line = next ? (next+1) : NULL;
}
return -2;
}
const char *janus_rtp_header_extension_get_from_id(const char *sdp, int id) {
if(!sdp || id < 0)
return NULL;
/* Look for the mapping */
char extmap[100];
g_snprintf(extmap, 100, "a=extmap:%d ", id);
const char *line = strstr(sdp, "m=");
while(line) {
char *next = strchr(line, '\n');
if(next) {
*next = '\0';
if(strstr(line, extmap)) {
/* Gotcha! */
char extension[100];
if(sscanf(line, "a=extmap:%d %s", &id, extension) == 2) {
*next = '\n';
if(strstr(extension, JANUS_RTP_EXTMAP_AUDIO_LEVEL))
return JANUS_RTP_EXTMAP_AUDIO_LEVEL;
if(strstr(extension, JANUS_RTP_EXTMAP_VIDEO_ORIENTATION))
return JANUS_RTP_EXTMAP_VIDEO_ORIENTATION;
if(strstr(extension, JANUS_RTP_EXTMAP_PLAYOUT_DELAY))
return JANUS_RTP_EXTMAP_PLAYOUT_DELAY;
if(strstr(extension, JANUS_RTP_EXTMAP_TOFFSET))
return JANUS_RTP_EXTMAP_TOFFSET;
if(strstr(extension, JANUS_RTP_EXTMAP_ABS_SEND_TIME))
return JANUS_RTP_EXTMAP_ABS_SEND_TIME;
if(strstr(extension, JANUS_RTP_EXTMAP_TRANSPORT_WIDE_CC))
return JANUS_RTP_EXTMAP_TRANSPORT_WIDE_CC;
if(strstr(extension, JANUS_RTP_EXTMAP_RTP_STREAM_ID))
return JANUS_RTP_EXTMAP_RTP_STREAM_ID;
JANUS_LOG(LOG_ERR, "Unsupported extension '%s'\n", extension);
return NULL;
}
}
*next = '\n';
}
line = next ? (next+1) : NULL;
}
return NULL;
}
/* Static helper to quickly find the extension data */
static int janus_rtp_header_extension_find(char *buf, int len, int id,
uint8_t *byte, uint32_t *word, char **ref) {
if(!buf || len < 12)
return -1;
janus_rtp_header *rtp = (janus_rtp_header *)buf;
int hlen = 12;
if(rtp->csrccount) /* Skip CSRC if needed */
hlen += rtp->csrccount*4;
if(rtp->extension) {
janus_rtp_header_extension *ext = (janus_rtp_header_extension *)(buf+hlen);
int extlen = ntohs(ext->length)*4;
hlen += 4;
if(len > (hlen + extlen)) {
/* 1-Byte extension */
if(ntohs(ext->type) == 0xBEDE) {
const uint8_t padding = 0x00, reserved = 0xF;
uint8_t extid = 0, idlen;
int i = 0;
while(i < extlen) {
extid = buf[hlen+i] >> 4;
if(extid == reserved) {
break;
} else if(extid == padding) {
i++;
continue;
}
idlen = (buf[hlen+i] & 0xF)+1;
if(extid == id) {
/* Found! */
if(byte)
*byte = buf[hlen+i+1];
if(word)
*word = ntohl(*(uint32_t *)(buf+hlen+i));
if(ref)
*ref = &buf[hlen+i];
return 0;
}
i += 1 + idlen;
}
}
hlen += extlen;
}
}
return -1;
}
int janus_rtp_header_extension_parse_audio_level(char *buf, int len, int id, int *level) {
uint8_t byte = 0;
if(janus_rtp_header_extension_find(buf, len, id, &byte, NULL, NULL) < 0)
return -1;
/* a=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level */
int v = (byte & 0x80) >> 7;
int value = byte & 0x7F;
JANUS_LOG(LOG_DBG, "%02x --> v=%d, level=%d\n", byte, v, value);
if(level)
*level = value;
return 0;
}
int janus_rtp_header_extension_parse_video_orientation(char *buf, int len, int id,
gboolean *c, gboolean *f, gboolean *r1, gboolean *r0) {
uint8_t byte = 0;
if(janus_rtp_header_extension_find(buf, len, id, &byte, NULL, NULL) < 0)
return -1;
/* a=extmap:4 urn:3gpp:video-orientation */
gboolean cbit = (byte & 0x08) >> 3;
gboolean fbit = (byte & 0x04) >> 2;
gboolean r1bit = (byte & 0x02) >> 1;
gboolean r0bit = byte & 0x01;
JANUS_LOG(LOG_DBG, "%02x --> c=%d, f=%d, r1=%d, r0=%d\n", byte, cbit, fbit, r1bit, r0bit);
if(c)
*c = cbit;
if(f)
*f = fbit;
if(r1)
*r1 = r1bit;
if(r0)
*r0 = r0bit;
return 0;
}
int janus_rtp_header_extension_parse_playout_delay(char *buf, int len, int id,
uint16_t *min_delay, uint16_t *max_delay) {
uint32_t bytes = 0;
if(janus_rtp_header_extension_find(buf, len, id, NULL, &bytes, NULL) < 0)
return -1;
/* a=extmap:6 http://www.webrtc.org/experiments/rtp-hdrext/playout-delay */
uint16_t min = (bytes & 0x00FFF000) >> 12;
uint16_t max = bytes & 0x00000FFF;
JANUS_LOG(LOG_DBG, "%"SCNu32"x --> min=%"SCNu16", max=%"SCNu16"\n", bytes, min, max);
if(min_delay)
*min_delay = min;
if(max_delay)
*max_delay = max;
return 0;
}
int janus_rtp_header_extension_parse_rtp_stream_id(char *buf, int len, int id,
char *sdes_item, int sdes_len) {
char *ext = NULL;
if(janus_rtp_header_extension_find(buf, len, id, NULL, NULL, &ext) < 0)
return -1;
/* a=extmap:3/sendonly urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id */
if(ext == NULL)
return -2;
int val_len = (*ext & 0x0F) + 1;
if(val_len > (sdes_len-1)) {
JANUS_LOG(LOG_WARN, "SDES buffer is too small (%d < %d), RTP stream ID will be cut\n", val_len, sdes_len);
val_len = sdes_len-1;
}
memcpy(sdes_item, ext+1, val_len);
*(sdes_item+val_len) = '\0';
return 0;
}
int janus_rtp_header_extension_parse_transport_wide_cc(char *buf, int len, int id, uint16_t *transSeqNum) {
uint32_t bytes = 0;
if(janus_rtp_header_extension_find(buf, len, id, NULL, &bytes, NULL) < 0)
return -1;
/* 0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| ID | L=1 |transport-wide sequence number | zero padding |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
*transSeqNum = (bytes & 0x00FFFF00) >> 8;
return 0;
}
/* RTP context related methods */
void janus_rtp_switching_context_reset(janus_rtp_switching_context *context) {
if(context == NULL)
return;
/* Reset the context values */
memset(context, 0, sizeof(*context));
}
int janus_rtp_skew_compensate_audio(janus_rtp_header *header, janus_rtp_switching_context *context, gint64 now) {
/* Reset values if a new ssrc has been detected */
if (context->a_new_ssrc) {
context->a_reference_time = now;
context->a_start_ts = 0;
context->a_start_time = 0;
context->a_active_delay = 0;
context->a_prev_delay = 0;
context->a_seq_offset = 0;
context->a_ts_offset = 0;
context->a_target_ts = 0;
context->a_new_ssrc = FALSE;
}
/* N : a N sequence number jump has been performed */
/* 0 : any new skew compensation has been applied */
/* -N : a N packet drop must be performed */
int exit_status = 0;
/* Do not execute skew analysis in the first seconds */
if (now-context->a_reference_time < SKEW_DETECTION_WAIT_TIME_SECS*G_USEC_PER_SEC) {
return 0;
} else if (!context->a_start_time) {
context->a_start_time = now;
context->a_start_ts = context->a_last_ts;
}
/* Skew analysis */
/* Are we waiting for a target timestamp? (a negative skew has been evaluated in a previous iteration) */
if (context->a_target_ts > 0 && (gint32)(context->a_target_ts - context->a_last_ts) > 0) {
context->a_seq_offset--;
exit_status = -1;
} else {
context->a_target_ts = 0;
/* Do not execute analysis for out of order packets or multi-packets frame */
if (context->a_last_seq == context->a_prev_seq + 1 && context->a_last_ts != context->a_prev_ts) {
/* Set the sample rate according to the header */
guint32 akhz = 48; /* 48khz for Opus */
if(header->type == 0 || header->type == 8 || header->type == 9)
akhz = 8;
/* Evaluate the local RTP timestamp according to the local clock */
guint32 expected_ts = ((now - context->a_start_time)*akhz)/1000 + context->a_start_ts;
/* Evaluate current delay */
gint32 delay_now = context->a_last_ts - expected_ts;
/* Exponentially weighted moving average estimation */
gint32 delay_estimate = (31*context->a_prev_delay + delay_now)/32;
/* Save previous delay for the next iteration*/
context->a_prev_delay = delay_estimate;
/* Evaluate the distance between active delay and current delay estimate */
gint32 offset = context->a_active_delay - delay_estimate;
JANUS_LOG(LOG_HUGE, "audio skew status SSRC=%"SCNu32" RECVD_TS=%"SCNu32" EXPTD_TS=%"SCNu32" OFFSET=%"SCNi32" TS_OFFSET=%"SCNi32" SEQ_OFFSET=%"SCNi16"\n", context->a_last_ssrc, context->a_last_ts, expected_ts, offset, context->a_ts_offset, context->a_seq_offset);
/* Check if the offset has surpassed the threshold */
gint32 skew_th = RTP_AUDIO_SKEW_TH_MS*akhz;
if (offset >= skew_th) {
/* The source is slowing down */
/* Update active delay */
context->a_active_delay = delay_estimate;
/* Adjust ts offset */
context->a_ts_offset += skew_th;
/* Calculate last ts increase */
guint32 ts_incr = context->a_last_ts-context->a_prev_ts;
/* Evaluate sequence number jump */
guint16 jump = (skew_th+ts_incr-1)/ts_incr;
/* Adjust seq num offset */
context->a_seq_offset += jump;
exit_status = jump;
} else if (offset <= -skew_th) {
/* The source is speeding up*/
/* Update active delay */
context->a_active_delay = delay_estimate;
/* Adjust ts offset */
context->a_ts_offset -= skew_th;
/* Set target ts */
context->a_target_ts = context->a_last_ts + skew_th;
if (context->a_target_ts == 0)
context->a_target_ts = 1;
/* Adjust seq num offset */
context->a_seq_offset--;
exit_status = -1;
}
}
}
/* Skew compensation */
/* Fix header timestamp considering the active offset */
guint32 fixed_rtp_ts = context->a_last_ts + context->a_ts_offset;
header->timestamp = htonl(fixed_rtp_ts);
/* Fix header sequence number considering the total offset */
guint16 fixed_rtp_seq = context->a_last_seq + context->a_seq_offset;
header->seq_number = htons(fixed_rtp_seq);
return exit_status;
}
int janus_rtp_skew_compensate_video(janus_rtp_header *header, janus_rtp_switching_context *context, gint64 now) {
/* Reset values if a new ssrc has been detected */
if (context->v_new_ssrc) {
context->v_reference_time = now;
context->v_start_ts = 0;
context->v_start_time = 0;
context->v_active_delay = 0;
context->v_prev_delay = 0;
context->v_seq_offset = 0;
context->v_ts_offset = 0;
context->v_target_ts = 0;
context->v_new_ssrc = FALSE;
}
/* N : a N sequence numbers jump has been performed */
/* 0 : any new skew compensation has been applied */
/* -N : a N packets drop must be performed */
int exit_status = 0;
/* Do not execute skew analysis in the first seconds */
if (now-context->v_reference_time < SKEW_DETECTION_WAIT_TIME_SECS*G_USEC_PER_SEC) {
return 0;
} else if (!context->v_start_time) {
context->v_start_time = now;
context->v_start_ts = context->v_last_ts;
}
/* Skew analysis */
/* Are we waiting for a target timestamp? (a negative skew has been evaluated in a previous iteration) */
if (context->v_target_ts > 0 && (gint32)(context->v_target_ts - context->v_last_ts) > 0) {
context->v_seq_offset--;
exit_status = -1;
} else {
context->v_target_ts = 0;
/* Do not execute analysis for out of order packets or multi-packets frame */
if (context->v_last_seq == context->v_prev_seq + 1 && context->v_last_ts != context->v_prev_ts) {
/* Set the sample rate */
guint32 vkhz = 90; /* 90khz */
/* Evaluate the local RTP timestamp according to the local clock */
guint32 expected_ts = ((now - context->v_start_time)*vkhz)/1000 + context->v_start_ts;
/* Evaluate current delay */
gint32 delay_now = context->v_last_ts - expected_ts;
/* Exponentially weighted moving average estimation */
gint32 delay_estimate = (31*context->v_prev_delay + delay_now)/32;
/* Save previous delay for the next iteration*/
context->v_prev_delay = delay_estimate;
/* Evaluate the distance between active delay and current delay estimate */
gint32 offset = context->v_active_delay - delay_estimate;
JANUS_LOG(LOG_HUGE, "video skew status SSRC=%"SCNu32" RECVD_TS=%"SCNu32" EXPTD_TS=%"SCNu32" OFFSET=%"SCNi32" TS_OFFSET=%"SCNi32" SEQ_OFFSET=%"SCNi16"\n", context->v_last_ssrc, context->v_last_ts, expected_ts, offset, context->v_ts_offset, context->v_seq_offset);
/* Check if the offset has surpassed the threshold */
gint32 skew_th = RTP_VIDEO_SKEW_TH_MS*vkhz;
if (offset >= skew_th) {
/* The source is slowing down */
/* Update active delay */
context->v_active_delay = delay_estimate;
/* Adjust ts offset */
context->v_ts_offset += skew_th;
/* Calculate last ts increase */
guint32 ts_incr = context->v_last_ts-context->v_prev_ts;
/* Evaluate sequence number jump */
guint16 jump = (skew_th+ts_incr-1)/ts_incr;
/* Adjust seq num offset */
context->v_seq_offset += jump;
exit_status = jump;
} else if (offset <= -skew_th) {
/* The source is speeding up*/
/* Update active delay */
context->v_active_delay = delay_estimate;
/* Adjust ts offset */
context->v_ts_offset -= skew_th;
/* Set target ts */
context->v_target_ts = context->v_last_ts + skew_th;
if (context->v_target_ts == 0)
context->v_target_ts = 1;
/* Adjust seq num offset */
context->v_seq_offset--;
exit_status = -1;
}
}
}
/* Skew compensation */
/* Fix header timestamp considering the active offset */
guint32 fixed_rtp_ts = context->v_last_ts + context->v_ts_offset;
header->timestamp = htonl(fixed_rtp_ts);
/* Fix header sequence number considering the total offset */
guint16 fixed_rtp_seq = context->v_last_seq + context->v_seq_offset;
header->seq_number = htons(fixed_rtp_seq);
return exit_status;
}
void janus_rtp_header_update(janus_rtp_header *header, janus_rtp_switching_context *context, gboolean video, int step) {
if(header == NULL || context == NULL)
return;
/* Note: while the step property is still there for compatibility reasons, to
* keep the signature as it was before, it's ignored: whenever there's a switch
* to take into account, we compute how much time passed between the last RTP
* packet with the old SSRC and this new one, and prepare a timestamp accordingly */
uint32_t ssrc = ntohl(header->ssrc);
uint32_t timestamp = ntohl(header->timestamp);
uint16_t seq = ntohs(header->seq_number);
if(video) {
if(ssrc != context->v_last_ssrc) {
/* Video SSRC changed: update both sequence number and timestamp */
JANUS_LOG(LOG_VERB, "Video SSRC changed, %"SCNu32" --> %"SCNu32"\n",
context->v_last_ssrc, ssrc);
context->v_last_ssrc = ssrc;
context->v_base_ts_prev = context->v_last_ts;
context->v_base_ts = timestamp;
context->v_base_seq_prev = context->v_last_seq;
context->v_base_seq = seq;
/* How much time since the last video RTP packet? We compute an offset accordingly */
if(context->v_last_time > 0) {
gint64 time_diff = janus_get_monotonic_time() - context->v_last_time;
time_diff = (time_diff*90)/1000; /* We're assuming 90khz here */
if(time_diff == 0)
time_diff = 1;
context->v_base_ts_prev += (guint32)time_diff;
context->v_last_ts += (guint32)time_diff;
JANUS_LOG(LOG_VERB, "Computed offset for video RTP timestamp: %"SCNu32"\n", (guint32)time_diff);
}
/* Reset skew compensation data */
context->v_new_ssrc = TRUE;
}
if(context->v_seq_reset) {
/* Video sequence number was paused for a while: just update that */
context->v_seq_reset = FALSE;
context->v_base_seq_prev = context->v_last_seq;
context->v_base_seq = seq;
}
/* Compute a coherent timestamp and sequence number */
context->v_prev_ts = context->v_last_ts;
context->v_last_ts = (timestamp-context->v_base_ts) + context->v_base_ts_prev;
context->v_prev_seq = context->v_last_seq;
context->v_last_seq = (seq-context->v_base_seq)+context->v_base_seq_prev+1;
/* Update the timestamp and sequence number in the RTP packet */
header->timestamp = htonl(context->v_last_ts);
header->seq_number = htons(context->v_last_seq);
/* Take note of when we last handled this RTP packet */
context->v_last_time = janus_get_monotonic_time();
} else {
if(ssrc != context->a_last_ssrc) {
/* Audio SSRC changed: update both sequence number and timestamp */
JANUS_LOG(LOG_VERB, "Audio SSRC changed, %"SCNu32" --> %"SCNu32"\n",
context->a_last_ssrc, ssrc);
context->a_last_ssrc = ssrc;
context->a_base_ts_prev = context->a_last_ts;
context->a_base_ts = timestamp;
context->a_base_seq_prev = context->a_last_seq;
context->a_base_seq = seq;
/* How much time since the last audio RTP packet? We compute an offset accordingly */
if(context->a_last_time > 0) {
gint64 time_diff = janus_get_monotonic_time() - context->a_last_time;
int akhz = 48;
if(header->type == 0 || header->type == 8 || header->type == 9)
akhz = 8; /* We're assuming 48khz here (Opus), unless it's G.711/G.722 (8khz) */
time_diff = (time_diff*akhz)/1000;
if(time_diff == 0)
time_diff = 1;
context->a_base_ts_prev += (guint32)time_diff;
context->a_prev_ts += (guint32)time_diff;
context->a_last_ts += (guint32)time_diff;
JANUS_LOG(LOG_VERB, "Computed offset for audio RTP timestamp: %"SCNu32"\n", (guint32)time_diff);
}
/* Reset skew compensation data */
context->a_new_ssrc = TRUE;
}
if(context->a_seq_reset) {
/* Audio sequence number was paused for a while: just update that */
context->a_seq_reset = FALSE;
context->a_base_seq_prev = context->a_last_seq;
context->a_base_seq = seq;
}
/* Compute a coherent timestamp and sequence number */
context->a_prev_ts = context->a_last_ts;
context->a_last_ts = (timestamp-context->a_base_ts) + context->a_base_ts_prev;
context->a_prev_seq = context->a_last_seq;
context->a_last_seq = (seq-context->a_base_seq)+context->a_base_seq_prev+1;
/* Update the timestamp and sequence number in the RTP packet */
header->timestamp = htonl(context->a_last_ts);
header->seq_number = htons(context->a_last_seq);
/* Take note of when we last handled this RTP packet */
context->a_last_time = janus_get_monotonic_time();
}
}
/* SRTP stuff: we may need our own randomizer */
#ifdef HAVE_SRTP_2
int srtp_crypto_get_random(uint8_t *key, int len) {
/* libsrtp 2.0 doesn't have crypto_get_random, we use OpenSSL's RAND_* to replace it:
* https://wiki.openssl.org/index.php/Random_Numbers */
int rc = RAND_bytes(key, len);
if(rc != 1) {
/* Error generating */
return -1;
}
return 0;
}
#endif
/* SRTP error codes as a string array */
static const char *janus_srtp_error[] =
{
#ifdef HAVE_SRTP_2
"srtp_err_status_ok",
"srtp_err_status_fail",
"srtp_err_status_bad_param",
"srtp_err_status_alloc_fail",
"srtp_err_status_dealloc_fail",
"srtp_err_status_init_fail",
"srtp_err_status_terminus",
"srtp_err_status_auth_fail",
"srtp_err_status_cipher_fail",
"srtp_err_status_replay_fail",
"srtp_err_status_replay_old",
"srtp_err_status_algo_fail",
"srtp_err_status_no_such_op",
"srtp_err_status_no_ctx",
"srtp_err_status_cant_check",
"srtp_err_status_key_expired",
"srtp_err_status_socket_err",
"srtp_err_status_signal_err",
"srtp_err_status_nonce_bad",
"srtp_err_status_read_fail",
"srtp_err_status_write_fail",
"srtp_err_status_parse_err",
"srtp_err_status_encode_err",
"srtp_err_status_semaphore_err",
"srtp_err_status_pfkey_err",
#else
"err_status_ok",
"err_status_fail",
"err_status_bad_param",
"err_status_alloc_fail",
"err_status_dealloc_fail",
"err_status_init_fail",
"err_status_terminus",
"err_status_auth_fail",
"err_status_cipher_fail",
"err_status_replay_fail",
"err_status_replay_old",
"err_status_algo_fail",
"err_status_no_such_op",
"err_status_no_ctx",
"err_status_cant_check",
"err_status_key_expired",
"err_status_socket_err",
"err_status_signal_err",
"err_status_nonce_bad",
"err_status_read_fail",
"err_status_write_fail",
"err_status_parse_err",
"err_status_encode_err",
"err_status_semaphore_err",
"err_status_pfkey_err",
#endif
};
const char *janus_srtp_error_str(int error) {
if(error < 0 || error > 24)
return NULL;
return janus_srtp_error[error];
}