Fork me on GitHub
rtp.h
Go to the documentation of this file.
1 
13 #ifndef JANUS_RTP_H
14 #define JANUS_RTP_H
15 
16 #include <arpa/inet.h>
17 #if defined (__MACH__) || defined(__FreeBSD__)
18 #include <machine/endian.h>
19 #define __BYTE_ORDER BYTE_ORDER
20 #define __BIG_ENDIAN BIG_ENDIAN
21 #define __LITTLE_ENDIAN LITTLE_ENDIAN
22 #else
23 #include <endian.h>
24 #endif
25 #include <inttypes.h>
26 #include <string.h>
27 #include <glib.h>
28 #include <jansson.h>
29 
30 #include "plugins/plugin.h"
31 #include "utils.h"
32 
33 #define RTP_HEADER_SIZE 12
34 
36 typedef struct rtp_header
37 {
38 #if __BYTE_ORDER == __BIG_ENDIAN
39  uint16_t version:2;
40  uint16_t padding:1;
41  uint16_t extension:1;
42  uint16_t csrccount:4;
43  uint16_t markerbit:1;
44  uint16_t type:7;
45 #elif __BYTE_ORDER == __LITTLE_ENDIAN
46  uint16_t csrccount:4;
47  uint16_t extension:1;
48  uint16_t padding:1;
49  uint16_t version:2;
50  uint16_t type:7;
51  uint16_t markerbit:1;
52 #endif
53  uint16_t seq_number;
54  uint32_t timestamp;
55  uint32_t ssrc;
56  uint32_t csrc[0];
57 } rtp_header;
59 
61 typedef struct janus_rtp_packet {
62  char *data;
63  gint length;
64  gint64 created;
69 
72  uint16_t type;
73  uint16_t length;
75 
77 typedef struct janus_rtp_rfc2833_payload {
78 #if __BYTE_ORDER == __BIG_ENDIAN
79  uint8_t event;
80  uint8_t end:1;
81  uint8_t reserved:1;
82  uint8_t volume:6;
83  uint16_t duration;
84 #elif __BYTE_ORDER == __LITTLE_ENDIAN
85  uint8_t event;
86  uint8_t volume:6;
87  uint8_t reserved:1;
88  uint8_t end:1;
89  uint16_t duration;
90 #endif
92 
94 #define JANUS_RTP_EXTMAP_AUDIO_LEVEL "urn:ietf:params:rtp-hdrext:ssrc-audio-level"
95 
96 #define JANUS_RTP_EXTMAP_TOFFSET "urn:ietf:params:rtp-hdrext:toffset"
97 
98 #define JANUS_RTP_EXTMAP_ABS_SEND_TIME "http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time"
99 
100 #define JANUS_RTP_EXTMAP_ABS_CAPTURE_TIME "http://www.webrtc.org/experiments/rtp-hdrext/abs-capture-time"
101 
102 #define JANUS_RTP_EXTMAP_VIDEO_ORIENTATION "urn:3gpp:video-orientation"
103 
104 #define JANUS_RTP_EXTMAP_TRANSPORT_WIDE_CC "http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01"
105 
106 #define JANUS_RTP_EXTMAP_PLAYOUT_DELAY "http://www.webrtc.org/experiments/rtp-hdrext/playout-delay"
107 
108 #define JANUS_RTP_EXTMAP_MID "urn:ietf:params:rtp-hdrext:sdes:mid"
109 
110 #define JANUS_RTP_EXTMAP_RID "urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id"
111 
112 #define JANUS_RTP_EXTMAP_REPAIRED_RID "urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id"
113 
114 #define JANUS_RTP_EXTMAP_DEPENDENCY_DESC "https://aomediacodec.github.io/av1-rtp-spec/#dependency-descriptor-rtp-header-extension"
115 
116 #define JANUS_RTP_EXTMAP_VIDEO_LAYERS "http://www.webrtc.org/experiments/rtp-hdrext/video-layers-allocation00"
117 
118 #define JANUS_RTP_EXTMAP_ENCRYPTED "urn:ietf:params:rtp-hdrext:encrypt"
119 int janus_rtp_extension_id(const char *type);
120 
121 
122 typedef enum janus_audiocodec {
135 const char *janus_audiocodec_name(janus_audiocodec acodec);
138 
139 typedef enum janus_videocodec {
147 const char *janus_videocodec_name(janus_videocodec vcodec);
150 
151 
155 gboolean janus_is_rtp(char *buf, guint len);
156 
162 char *janus_rtp_payload(char *buf, int len, int *plen);
163 
168 int janus_rtp_header_extension_get_id(const char *sdp, const char *extension);
169 
175 const char *janus_rtp_header_extension_get_from_id(const char *sdp, int id);
176 
186 int janus_rtp_header_extension_parse_audio_level(char *buf, int len, int id, gboolean *vad, int *level);
187 
197 int janus_rtp_header_extension_parse_video_orientation(char *buf, int len, int id,
198  gboolean *c, gboolean *f, gboolean *r1, gboolean *r0);
199 
207 int janus_rtp_header_extension_parse_playout_delay(char *buf, int len, int id,
208  uint16_t *min_delay, uint16_t *max_delay);
209 
217 int janus_rtp_header_extension_parse_mid(char *buf, int len, int id,
218  char *sdes_item, int sdes_len);
219 
227 int janus_rtp_header_extension_parse_rid(char *buf, int len, int id,
228  char *sdes_item, int sdes_len);
229 
237 int janus_rtp_header_extension_parse_dependency_desc(char *buf, int len, int id,
238  uint8_t *dd_item, int *dd_len);
239 
246 int janus_rtp_header_extension_parse_abs_send_time(char *buf, int len, int id, uint32_t *abs_ts);
247 
254 int janus_rtp_header_extension_set_abs_send_time(char *buf, int len, int id, uint32_t abs_ts);
255 
262 int janus_rtp_header_extension_parse_abs_capture_time(char *buf, int len, int id, uint64_t *abs_ts);
263 
270 int janus_rtp_header_extension_set_abs_capture_time(char *buf, int len, int id, uint64_t abs_ts);
271 
278 int janus_rtp_header_extension_parse_transport_wide_cc(char *buf, int len, int id, uint16_t *transSeqNum);
279 
286 int janus_rtp_header_extension_set_transport_wide_cc(char *buf, int len, int id, uint16_t transSeqNum);
287 
295 int janus_rtp_header_extension_parse_video_layers_allocation(char *buf, int len, int id,
296  int8_t *spatial_layers, int8_t *temporal_layers);
297 
305 int janus_rtp_header_extension_replace_id(char *buf, int len, int id, int new_id);
306 
312  gint16 seq_offset;
316 
320 
326 void janus_rtp_header_update(janus_rtp_header *header, janus_rtp_switching_context *context, gboolean video, int step);
327 
328 #define RTP_AUDIO_SKEW_TH_MS 120
329 #define RTP_VIDEO_SKEW_TH_MS 120
330 #define SKEW_DETECTION_WAIT_TIME_SECS 10
331 
344 
345 
348 
350 typedef struct janus_av1_svc_context {
352  uint8_t tcnt;
354  uint8_t tioff;
356  GHashTable *templates;
360  gboolean updated;
362 
366 typedef struct janus_av1_svc_template {
368  uint8_t id;
370  int spatial;
372  int temporal;
374 
378 
391  uint8_t *dd, int dd_len, uint8_t *template_id, uint8_t *ebit);
393 
396 
412  guint32 drop_trigger;
414  gint64 last_relayed;
420  gboolean need_pli;
422 
426 
433 void janus_rtp_simulcasting_prepare(json_t *simulcast, int *rid_ext_id, uint32_t *ssrcs, char **rids);
434 
441 void janus_rtp_simulcasting_cleanup(int *rid_ext_id, uint32_t *ssrcs, char **rids, janus_mutex *rid_mutex);
442 
458  char *buf, int len, uint8_t *dd_content, int dd_len, uint32_t *ssrcs, char **rids,
461 
464 
466 typedef struct janus_rtp_svc_context {
470  int spatial;
474  int temporal;
478  guint32 drop_trigger;
482  gboolean changed_spatial;
486  gboolean need_pli;
488 
492 
506  char *buf, int len, uint8_t *dd_content, int dd_len,
509 
510 #endif
gboolean changed_temporal
Whether the temporal layer has changed after processing a packet.
Definition: rtp.h:418
Definition: rtp.h:140
int templayer_target
As above, but to handle transitions (e.g., wait for keyframe)
Definition: rtp.h:410
gboolean changed_spatial
Whether the spatial layer has changed after processing a packet.
Definition: rtp.h:482
gint64 evaluating_start_time
Definition: rtp.h:314
gboolean ts_reset
Definition: rtp.h:311
gboolean new_ssrc
Definition: rtp.h:311
uint32_t base_ts_prev
Definition: rtp.h:309
int janus_rtp_header_extension_parse_audio_level(char *buf, int len, int id, gboolean *vad, int *level)
Helper to parse a ssrc-audio-level RTP extension (https://tools.ietf.org/html/rfc6464) ...
Definition: rtp.c:230
void janus_rtp_svc_context_reset(janus_rtp_svc_context *context)
Set (or reset) the context fields to their default values.
Definition: rtp.c:1433
Helper struct to track SVC templates.
Definition: rtp.h:366
uint32_t timestamp
Definition: rtp.h:54
struct json_t json_t
Definition: plugin.h:236
Helper struct for processing and tracking VP9-SVC streams.
Definition: rtp.h:466
Helper struct for processing and tracking AV1-SVC streams.
Definition: rtp.h:350
gint64 created
Definition: rtp.h:64
int janus_rtp_header_extension_parse_mid(char *buf, int len, int id, char *sdes_item, int sdes_len)
Helper to parse a sdes-mid RTP extension (https://tools.ietf.org/html/draft-ietf-mmusic-sdp-bundle-ne...
Definition: rtp.c:286
Definition: rtp.h:129
uint16_t duration
Definition: rtp.h:83
janus_plugin_rtp_extensions extensions
Definition: rtp.h:67
gint64 last_relayed
When we relayed the last packet (used to detect when substreams become unavailable) ...
Definition: rtp.h:414
gint rid_ext_id
RTP Stream extension ID, if any.
Definition: rtp.h:400
Definition: rtp.h:131
int janus_rtp_header_extension_replace_id(char *buf, int len, int id, int new_id)
Helper to replace the ID of an RTP extension with a different one (e.g., to turn a repaired-rtp-strea...
Definition: rtp.c:515
int janus_videocodec_pt(janus_videocodec vcodec)
Definition: rtp.c:1119
uint16_t markerbit
Definition: rtp.h:43
int templayer
Which simulcast temporal layer we should forward back.
Definition: rtp.h:408
GMutex janus_mutex
Janus mutex implementation.
Definition: mutex.h:73
Definition: rtp.h:130
int janus_rtp_skew_compensate_video(janus_rtp_header *header, janus_rtp_switching_context *context, gint64 now)
Use the context info to compensate for video source skew, if needed.
Definition: rtp.c:730
Plugin-Core communication (implementation)
gboolean need_pli
Whether we need to send the user a keyframe request (PLI)
Definition: rtp.h:486
uint32_t prev_ts
Definition: rtp.h:309
gboolean changed_substream
Whether the substream has changed after processing a packet.
Definition: rtp.h:416
Definition: rtp.h:141
gboolean seq_reset
Definition: rtp.h:311
int janus_rtp_header_extension_parse_video_orientation(char *buf, int len, int id, gboolean *c, gboolean *f, gboolean *r1, gboolean *r0)
Helper to parse a video-orientation RTP extension (http://www.3gpp.org/ftp/Specs/html-info/26114.htm)
Definition: rtp.c:245
uint16_t base_seq_prev
Definition: rtp.h:310
int temporal_layers
Definition: rtp.h:358
janus_videocodec
Definition: rtp.h:139
uint32_t last_ts
Definition: rtp.h:309
uint8_t reserved
Definition: rtp.h:81
struct janus_rtp_header_extension janus_rtp_header_extension
RTP extension.
int janus_rtp_header_extension_parse_rid(char *buf, int len, int id, char *sdes_item, int sdes_len)
Helper to parse a rtp-stream-id RTP extension (https://tools.ietf.org/html/draft-ietf-avtext-rid-09) ...
Definition: rtp.c:307
char * janus_rtp_payload(char *buf, int len, int *plen)
Helper to quickly access the RTP payload, skipping header and extensions.
Definition: rtp.c:38
uint32_t ssrc
Definition: rtp.h:55
gint32 active_delay
Definition: rtp.h:313
uint8_t tioff
Template ID offset, as advertised via Dependency Descriptor.
Definition: rtp.h:354
VP9 SVC info, as parsed from a payload descriptor.
Definition: utils.h:417
struct janus_rtp_svc_context janus_rtp_svc_context
Helper struct for processing and tracking VP9-SVC streams.
gint32 ts_offset
Definition: rtp.h:313
uint16_t extension
Definition: rtp.h:41
int janus_rtp_header_extension_set_abs_send_time(char *buf, int len, int id, uint32_t abs_ts)
Helper to set an abs-send-time RTP extension (http://www.webrtc.org/experiments/rtp-hdrext/abs-send-t...
Definition: rtp.c:369
uint16_t seq_number
Definition: rtp.h:53
const char * janus_videocodec_name(janus_videocodec vcodec)
Definition: rtp.c:1084
void janus_rtp_simulcasting_prepare(json_t *simulcast, int *rid_ext_id, uint32_t *ssrcs, char **rids)
Helper method to prepare the simulcasting info (rids and/or SSRCs) from the simulcast object the core...
Definition: rtp.c:1153
TURN REST API client.
gboolean janus_rtp_svc_context_process_rtp(janus_rtp_svc_context *context, char *buf, int len, uint8_t *dd_content, int dd_len, janus_videocodec vcodec, janus_vp9_svc_info *info, janus_rtp_switching_context *sc)
Process an RTP packet, and decide whether this should be relayed or not, updating the context accordi...
Definition: rtp.c:1443
gint16 seq_offset
Definition: rtp.h:312
uint32_t base_ts
Definition: rtp.h:309
int temporal
Temporal layer associated to this template.
Definition: rtp.h:372
Definition: rtp.h:123
Definition: rtp.h:144
gboolean janus_is_rtp(char *buf, guint len)
Helper method to demultiplex RTP from other protocols.
Definition: rtp.c:31
struct rtp_header rtp_header
RTP Header (http://tools.ietf.org/html/rfc3550#section-5.1)
struct janus_rtp_simulcasting_context janus_rtp_simulcasting_context
Helper struct for processing and tracking simulcast streams.
int substream_target_temp
Definition: rtp.h:406
uint8_t end
Definition: rtp.h:80
int janus_rtp_header_extension_parse_transport_wide_cc(char *buf, int len, int id, uint16_t *transSeqNum)
Helper to parse a transport wide sequence number (https://tools.ietf.org/html/draft-holmer-rmcat-tran...
Definition: rtp.c:414
uint32_t start_ts
Definition: rtp.h:309
uint16_t csrccount
Definition: rtp.h:42
uint8_t id
Template ID.
Definition: rtp.h:368
janus_audiocodec
Definition: rtp.h:122
RTP packet.
Definition: rtp.h:61
void janus_rtp_header_update(janus_rtp_header *header, janus_rtp_switching_context *context, gboolean video, int step)
Use the context info to update the RTP header of a packet, if needed.
Definition: rtp.c:844
int janus_rtp_skew_compensate_audio(janus_rtp_header *header, janus_rtp_switching_context *context, gint64 now)
Use the context info to compensate for audio source skew, if needed.
Definition: rtp.c:614
janus_videocodec janus_videocodec_from_name(const char *name)
Definition: rtp.c:1103
janus_av1_svc_context av1_context[3]
Dependency Descriptors contexts, if any.
Definition: rtp.h:402
guint32 drop_trigger
How much time (in us, default 250000) without receiving packets will make us drop to the substream be...
Definition: rtp.h:412
int spatial
Which SVC spatial layer we should forward back.
Definition: rtp.h:470
RTP context, in order to make sure SSRC changes result in coherent seq/ts increases.
Definition: rtp.h:308
Definition: rtp.h:127
gboolean need_pli
Whether we need to send the user a keyframe request (PLI)
Definition: rtp.h:420
int substream
Which simulcast substream we should forward back.
Definition: rtp.h:404
gint64 start_time
Definition: rtp.h:314
uint16_t padding
Definition: rtp.h:40
Definition: rtp.h:126
RTP RFC2833 payload.
Definition: rtp.h:77
Definition: rtp.h:125
gint64 current_backoff
Definition: rtp.h:66
int temporal_target
As above, but to handle transitions (e.g., wait for keyframe)
Definition: rtp.h:476
gint64 last_retransmit
Definition: rtp.h:65
GHashTable * templates
Map of templates advertised via Dependency Descriptor, indexed by ID.
Definition: rtp.h:356
uint16_t prev_seq
Definition: rtp.h:310
int spatial
Spatial layer associated to this template.
Definition: rtp.h:370
int temporal
Which SVC temporal layer we should forward back.
Definition: rtp.h:474
int janus_rtp_header_extension_set_abs_capture_time(char *buf, int len, int id, uint64_t abs_ts)
Helper to set an abs-capture-time RTP extension (http://www.webrtc.org/experiments/rtp-hdrext/abs-cap...
Definition: rtp.c:400
void janus_rtp_simulcasting_cleanup(int *rid_ext_id, uint32_t *ssrcs, char **rids, janus_mutex *rid_mutex)
Helper method to cleanup some or all of the simulcasting info (rids and/or SSRCs) we may have prepare...
Definition: rtp.c:1183
RTP extension.
Definition: rtp.h:71
Definition: rtp.h:124
uint8_t tcnt
Number of templates advertised via Dependency Descriptor.
Definition: rtp.h:352
gint64 reference_time
Definition: rtp.h:314
Definition: rtp.h:142
struct janus_rtp_packet janus_rtp_packet
RTP packet.
uint8_t volume
Definition: rtp.h:82
guint32 drop_trigger
How much time (in us, default 250000) without receiving packets will make us drop to the substream be...
Definition: rtp.h:478
void janus_av1_svc_context_reset(janus_av1_svc_context *context)
Set (or reset) the context fields to their default values.
Definition: rtp.c:1678
uint8_t event
Definition: rtp.h:79
janus_audiocodec janus_audiocodec_from_name(const char *name)
Definition: rtp.c:1028
int substream_target
As above, but to handle transitions (e.g., wait for keyframe, or get this if available) ...
Definition: rtp.h:406
struct janus_rtp_switching_context janus_rtp_switching_context
RTP context, in order to make sure SSRC changes result in coherent seq/ts increases.
gint64 last_spatial_layer[3]
When we relayed the last packet (used to detect when layers become unavailable)
Definition: rtp.h:480
int janus_rtp_header_extension_set_transport_wide_cc(char *buf, int len, int id, uint16_t transSeqNum)
Helper to set a transport wide sequence number (https://tools.ietf.org/html/draft-holmer-rmcat-transp...
Definition: rtp.c:434
struct janus_av1_svc_template janus_av1_svc_template
Helper struct to track SVC templates.
uint32_t target_ts
Definition: rtp.h:309
uint16_t version
Definition: rtp.h:39
Janus plugin RTP extensions.
Definition: plugin.h:562
RTP Header (http://tools.ietf.org/html/rfc3550#section-5.1)
Definition: rtp.h:36
int janus_rtp_header_extension_parse_abs_send_time(char *buf, int len, int id, uint32_t *abs_ts)
Helper to parse an abs-send-time RTP extension (http://www.webrtc.org/experiments/rtp-hdrext/abs-send...
Definition: rtp.c:352
int spatial_target
As above, but to handle transitions (e.g., wait for keyframe, or get this if available) ...
Definition: rtp.h:472
uint16_t length
Definition: rtp.h:73
int janus_audiocodec_pt(janus_audiocodec acodec)
Definition: rtp.c:1054
Definition: rtp.h:132
struct janus_av1_svc_context dd_context
Dependency Descriptor context, in case it&#39;s needed.
Definition: rtp.h:468
int janus_rtp_header_extension_parse_video_layers_allocation(char *buf, int len, int id, int8_t *spatial_layers, int8_t *temporal_layers)
Helper to parse a video layers allocation (http://www.webrtc.org/experiments/rtp-hdrext/video-layers-...
Definition: rtp.c:448
int janus_rtp_header_extension_parse_dependency_desc(char *buf, int len, int id, uint8_t *dd_item, int *dd_len)
Helper to parse a dependency descriptor RTP extension (https://aomediacodec.github.io/av1-rtp-spec/#dependency-descriptor-rtp-header-extension)
Definition: rtp.c:329
Definition: rtp.h:133
rtp_header janus_rtp_header
Definition: rtp.h:58
Definition: rtp.h:143
char * data
Definition: rtp.h:62
uint32_t csrc[0]
Definition: rtp.h:56
gboolean updated
Whether this context changed since the last update.
Definition: rtp.h:360
const char * janus_rtp_header_extension_get_from_id(const char *sdp, int id)
Ugly and dirty helper to quickly get the RTP extension namespace associated with an id (extmap) in an...
Definition: rtp.c:93
int spatial_layers
How many spatial and temporal layers are available.
Definition: rtp.h:358
int janus_rtp_header_extension_get_id(const char *sdp, const char *extension)
Ugly and dirty helper to quickly get the id associated with an RTP extension (extmap) in an SDP...
Definition: rtp.c:64
Definition: rtp.h:145
gboolean changed_temporal
Whether the temporal layer has changed after processing a packet.
Definition: rtp.h:484
Helper struct for processing and tracking simulcast streams.
Definition: rtp.h:398
gint32 prev_delay
Definition: rtp.h:313
void janus_rtp_switching_context_reset(janus_rtp_switching_context *context)
Set (or reset) the context fields to their default values.
Definition: rtp.c:607
uint16_t type
Definition: rtp.h:72
int janus_rtp_extension_id(const char *type)
Definition: rtp.c:580
gint length
Definition: rtp.h:63
uint16_t last_seq
Definition: rtp.h:310
uint16_t base_seq
Definition: rtp.h:310
Definition: rtp.h:128
const char * janus_audiocodec_name(janus_audiocodec acodec)
Definition: rtp.c:999
int janus_rtp_header_extension_parse_playout_delay(char *buf, int len, int id, uint16_t *min_delay, uint16_t *max_delay)
Helper to parse a playout-delay RTP extension (https://webrtc.org/experiments/rtp-hdrext/playout-dela...
Definition: rtp.c:267
gint64 last_time
Definition: rtp.h:314
uint16_t type
Definition: rtp.h:44
struct janus_av1_svc_context janus_av1_svc_context
Helper struct for processing and tracking AV1-SVC streams.
int janus_rtp_header_extension_parse_abs_capture_time(char *buf, int len, int id, uint64_t *abs_ts)
Helper to parse an abs-capture-time RTP extension (http://www.webrtc.org/experiments/rtp-hdrext/abs-c...
Definition: rtp.c:383
struct janus_rtp_rfc2833_payload janus_rtp_rfc2833_payload
RTP RFC2833 payload.
gboolean janus_av1_svc_context_process_dd(janus_av1_svc_context *context, uint8_t *dd, int dd_len, uint8_t *template_id, uint8_t *ebit)
Process a Dependency Descriptor payload, updating the SVC context accordingly.
Definition: rtp.c:1687
uint32_t last_ssrc
Definition: rtp.h:309
void janus_rtp_simulcasting_context_reset(janus_rtp_simulcasting_context *context)
Set (or reset) the context fields to their default values.
Definition: rtp.c:1139
gboolean janus_rtp_simulcasting_context_process_rtp(janus_rtp_simulcasting_context *context, char *buf, int len, uint8_t *dd_content, int dd_len, uint32_t *ssrcs, char **rids, janus_videocodec vcodec, janus_rtp_switching_context *sc, janus_mutex *rid_mutex)
Process an RTP packet, and decide whether this should be relayed or not, updating the context accordi...
Definition: rtp.c:1203