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 
32 #define RTP_HEADER_SIZE 12
33 
35 typedef struct rtp_header
36 {
37 #if __BYTE_ORDER == __BIG_ENDIAN
38  uint16_t version:2;
39  uint16_t padding:1;
40  uint16_t extension:1;
41  uint16_t csrccount:4;
42  uint16_t markerbit:1;
43  uint16_t type:7;
44 #elif __BYTE_ORDER == __LITTLE_ENDIAN
45  uint16_t csrccount:4;
46  uint16_t extension:1;
47  uint16_t padding:1;
48  uint16_t version:2;
49  uint16_t type:7;
50  uint16_t markerbit:1;
51 #endif
52  uint16_t seq_number;
53  uint32_t timestamp;
54  uint32_t ssrc;
55  uint32_t csrc[16];
58 
60 typedef struct janus_rtp_packet {
61  char *data;
62  gint length;
63  gint64 created;
67 
70  uint16_t type;
71  uint16_t length;
73 
75 #define JANUS_RTP_EXTMAP_AUDIO_LEVEL "urn:ietf:params:rtp-hdrext:ssrc-audio-level"
77 #define JANUS_RTP_EXTMAP_TOFFSET "urn:ietf:params:rtp-hdrext:toffset"
79 #define JANUS_RTP_EXTMAP_ABS_SEND_TIME "http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time"
81 #define JANUS_RTP_EXTMAP_VIDEO_ORIENTATION "urn:3gpp:video-orientation"
83 #define JANUS_RTP_EXTMAP_TRANSPORT_WIDE_CC "http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01"
85 #define JANUS_RTP_EXTMAP_PLAYOUT_DELAY "http://www.webrtc.org/experiments/rtp-hdrext/playout-delay"
87 #define JANUS_RTP_EXTMAP_MID "urn:ietf:params:rtp-hdrext:sdes:mid"
89 #define JANUS_RTP_EXTMAP_RID "urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id"
91 #define JANUS_RTP_EXTMAP_REPAIRED_RID "urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id"
93 #define JANUS_RTP_EXTMAP_DEPENDENCY_DESC "https://aomediacodec.github.io/av1-rtp-spec/#dependency-descriptor-rtp-header-extension"
95 #define JANUS_RTP_EXTMAP_ENCRYPTED "urn:ietf:params:rtp-hdrext:encrypt"
96 
97 
98 typedef enum janus_audiocodec {
109 const char *janus_audiocodec_name(janus_audiocodec acodec);
112 
113 typedef enum janus_videocodec {
121 const char *janus_videocodec_name(janus_videocodec vcodec);
124 
125 
129 gboolean janus_is_rtp(char *buf, guint len);
130 
136 char *janus_rtp_payload(char *buf, int len, int *plen);
137 
142 int janus_rtp_header_extension_get_id(const char *sdp, const char *extension);
143 
149 const char *janus_rtp_header_extension_get_from_id(const char *sdp, int id);
150 
160 int janus_rtp_header_extension_parse_audio_level(char *buf, int len, int id, gboolean *vad, int *level);
161 
171 int janus_rtp_header_extension_parse_video_orientation(char *buf, int len, int id,
172  gboolean *c, gboolean *f, gboolean *r1, gboolean *r0);
173 
181 int janus_rtp_header_extension_parse_playout_delay(char *buf, int len, int id,
182  uint16_t *min_delay, uint16_t *max_delay);
183 
191 int janus_rtp_header_extension_parse_mid(char *buf, int len, int id,
192  char *sdes_item, int sdes_len);
193 
201 int janus_rtp_header_extension_parse_rid(char *buf, int len, int id,
202  char *sdes_item, int sdes_len);
203 
211 int janus_rtp_header_extension_parse_dependency_desc(char *buf, int len, int id,
212  uint8_t *dd_item, int *dd_len);
213 
220 int janus_rtp_header_extension_parse_abs_sent_time(char *buf, int len, int id, uint32_t *abs_ts);
221 
228 int janus_rtp_header_extension_set_abs_send_time(char *buf, int len, int id, uint32_t abs_ts);
229 
236 int janus_rtp_header_extension_parse_transport_wide_cc(char *buf, int len, int id, uint16_t *transSeqNum);
237 
244 int janus_rtp_header_extension_set_transport_wide_cc(char *buf, int len, int id, uint16_t transSeqNum);
245 
253 int janus_rtp_header_extension_replace_id(char *buf, int len, int id, int new_id);
254 
263  gint16 a_seq_offset,
270 
274 
280 void janus_rtp_header_update(janus_rtp_header *header, janus_rtp_switching_context *context, gboolean video, int step);
281 
282 #define RTP_AUDIO_SKEW_TH_MS 120
283 #define RTP_VIDEO_SKEW_TH_MS 120
284 #define SKEW_DETECTION_WAIT_TIME_SECS 10
285 
298 
299 
303 
316  guint32 drop_trigger;
318  gint64 last_relayed;
324  gboolean need_pli;
326 
330 
337 void janus_rtp_simulcasting_prepare(json_t *simulcast, int *rid_ext_id, uint32_t *ssrcs, char **rids);
338 
351  char *buf, int len, uint32_t *ssrcs, char **rids,
354 
358 
359 typedef struct janus_av1_svc_context {
361  uint8_t tcnt;
363  uint8_t tioff;
365  GHashTable *templates;
369  gboolean updated;
371 
375 typedef struct janus_av1_svc_template {
377  uint8_t id;
379  int spatial;
381  int temporal;
383 
387 
399  uint8_t *dd, int dd_len, uint8_t *template_id);
401 
402 
403 #endif
Plugin-Core communication (implementation)
struct json_t json_t
Definition: plugin.h:236
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:353
struct janus_rtp_packet janus_rtp_packet
RTP packet.
char * janus_rtp_payload(char *buf, int len, int *plen)
Helper to quickly access the RTP payload, skipping header and extensions.
Definition: rtp.c:26
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:474
struct janus_rtp_header_extension janus_rtp_header_extension
RTP extension.
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:704
void janus_av1_svc_context_reset(janus_av1_svc_context *context)
Set (or reset) the context fields to their default values.
Definition: rtp.c:1222
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:81
struct janus_rtp_switching_context janus_rtp_switching_context
RTP context, in order to make sure SSRC changes result in coherent seq/ts increases.
void janus_rtp_switching_context_reset(janus_rtp_switching_context *context)
Set (or reset) the context fields to their default values.
Definition: rtp.c:467
janus_audiocodec
Definition: rtp.h:98
@ JANUS_AUDIOCODEC_PCMU
Definition: rtp.h:103
@ JANUS_AUDIOCODEC_MULTIOPUS
Definition: rtp.h:101
@ JANUS_AUDIOCODEC_OPUSRED
Definition: rtp.h:102
@ JANUS_AUDIOCODEC_NONE
Definition: rtp.h:99
@ JANUS_AUDIOCODEC_ISAC_32K
Definition: rtp.h:106
@ JANUS_AUDIOCODEC_G722
Definition: rtp.h:105
@ JANUS_AUDIOCODEC_OPUS
Definition: rtp.h:100
@ JANUS_AUDIOCODEC_PCMA
Definition: rtp.h:104
@ JANUS_AUDIOCODEC_ISAC_16K
Definition: rtp.h:107
void janus_rtp_simulcasting_context_reset(janus_rtp_simulcasting_context *context)
Set (or reset) the context fields to their default values.
Definition: rtp.c:1032
gboolean janus_rtp_simulcasting_context_process_rtp(janus_rtp_simulcasting_context *context, char *buf, int len, uint32_t *ssrcs, char **rids, janus_videocodec vcodec, 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:1073
const char * janus_audiocodec_name(janus_audiocodec acodec)
Definition: rtp.c:904
janus_audiocodec janus_audiocodec_from_name(const char *name)
Definition: rtp.c:929
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....
Definition: rtp.c:313
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:401
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:387
janus_videocodec
Definition: rtp.h:113
@ JANUS_VIDEOCODEC_NONE
Definition: rtp.h:114
@ JANUS_VIDEOCODEC_H264
Definition: rtp.h:117
@ JANUS_VIDEOCODEC_AV1
Definition: rtp.h:118
@ JANUS_VIDEOCODEC_VP9
Definition: rtp.h:116
@ JANUS_VIDEOCODEC_VP8
Definition: rtp.h:115
@ JANUS_VIDEOCODEC_H265
Definition: rtp.h:119
int janus_rtp_header_extension_parse_abs_sent_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:336
int janus_videocodec_pt(janus_videocodec vcodec)
Definition: rtp.c:1012
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:251
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:367
gboolean janus_av1_svc_context_process_dd(janus_av1_svc_context *context, uint8_t *dd, int dd_len, uint8_t *template_id)
Process a Dependency Descriptor payload, updating the SVC context accordingly.
Definition: rtp.c:1231
rtp_header janus_rtp_header
Definition: rtp.h:57
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:270
struct janus_av1_svc_template janus_av1_svc_template
Helper struct to track SVC templates.
janus_videocodec janus_videocodec_from_name(const char *name)
Definition: rtp.c:996
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:291
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....
Definition: rtp.c:229
struct rtp_header rtp_header
RTP Header (http://tools.ietf.org/html/rfc3550#section-5.1)
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:1043
struct janus_av1_svc_context janus_av1_svc_context
Helper struct for processing and tracking AV1-SVC streams.
int janus_audiocodec_pt(janus_audiocodec acodec)
Definition: rtp.c:951
gboolean janus_is_rtp(char *buf, guint len)
Helper method to demultiplex RTP from other protocols.
Definition: rtp.c:19
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:52
struct janus_rtp_simulcasting_context janus_rtp_simulcasting_context
Helper struct for processing and tracking simulcast streams.
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:214
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:590
const char * janus_videocodec_name(janus_videocodec vcodec)
Definition: rtp.c:977
Helper struct for processing and tracking AV1-SVC streams.
Definition: rtp.h:359
uint8_t tcnt
Number of templates advertised via Dependency Descriptor.
Definition: rtp.h:361
int spatial_layers
How many spatial and temporal layers are available.
Definition: rtp.h:367
GHashTable * templates
Map of templates advertised via Dependency Descriptor, indexed by ID.
Definition: rtp.h:365
gboolean updated
Whether this context changed since the last update.
Definition: rtp.h:369
uint8_t tioff
Template ID offset, as advertised via Dependency Descriptor.
Definition: rtp.h:363
int temporal_layers
Definition: rtp.h:367
Helper struct to track SVC templates.
Definition: rtp.h:375
uint8_t id
Template ID.
Definition: rtp.h:377
int spatial
Spatial layer associated to this template.
Definition: rtp.h:379
int temporal
Temporal layer associated to this template.
Definition: rtp.h:381
Janus plugin RTP extensions.
Definition: plugin.h:550
RTP extension.
Definition: rtp.h:69
uint16_t length
Definition: rtp.h:71
uint16_t type
Definition: rtp.h:70
RTP packet.
Definition: rtp.h:60
char * data
Definition: rtp.h:61
gint64 last_retransmit
Definition: rtp.h:64
gint length
Definition: rtp.h:62
gint64 created
Definition: rtp.h:63
janus_plugin_rtp_extensions extensions
Definition: rtp.h:65
Helper struct for processing and tracking simulcast streams.
Definition: rtp.h:304
gboolean changed_temporal
Whether the temporal layer has changed after processing a packet.
Definition: rtp.h:322
int templayer_target
As above, but to handle transitions (e.g., wait for keyframe)
Definition: rtp.h:314
int substream_target
As above, but to handle transitions (e.g., wait for keyframe, or get this if available)
Definition: rtp.h:310
gint rid_ext_id
RTP Stream extension ID, if any.
Definition: rtp.h:306
gboolean changed_substream
Whether the substream has changed after processing a packet.
Definition: rtp.h:320
guint32 drop_trigger
How much time (in us, default 250000) without receiving packets will make us drop to the substream be...
Definition: rtp.h:316
int substream_target_temp
Definition: rtp.h:310
int templayer
Which simulcast temporal layer we should forward back.
Definition: rtp.h:312
int substream
Which simulcast substream we should forward back.
Definition: rtp.h:308
gboolean need_pli
Whether we need to send the user a keyframe request (PLI)
Definition: rtp.h:324
gint64 last_relayed
When we relayed the last packet (used to detect when substreams become unavailable)
Definition: rtp.h:318
RTP context, in order to make sure SSRC changes result in coherent seq/ts increases.
Definition: rtp.h:256
uint32_t v_start_ts
Definition: rtp.h:258
uint32_t a_prev_ts
Definition: rtp.h:257
gboolean a_seq_reset
Definition: rtp.h:261
gboolean v_seq_reset
Definition: rtp.h:262
uint32_t v_base_ts
Definition: rtp.h:258
gint64 a_reference_time
Definition: rtp.h:267
gint32 v_prev_delay
Definition: rtp.h:266
gint32 v_ts_offset
Definition: rtp.h:266
uint16_t v_base_seq_prev
Definition: rtp.h:260
gint32 v_active_delay
Definition: rtp.h:266
uint32_t a_last_ssrc
Definition: rtp.h:257
gboolean a_ts_reset
Definition: rtp.h:261
uint16_t a_base_seq
Definition: rtp.h:259
uint16_t v_base_seq
Definition: rtp.h:260
uint32_t a_target_ts
Definition: rtp.h:257
gint64 a_evaluating_start_time
Definition: rtp.h:267
uint32_t a_start_ts
Definition: rtp.h:257
gboolean a_new_ssrc
Definition: rtp.h:261
gboolean v_ts_reset
Definition: rtp.h:262
uint32_t a_base_ts
Definition: rtp.h:257
uint32_t a_base_ts_prev
Definition: rtp.h:257
gboolean v_new_ssrc
Definition: rtp.h:262
gint64 v_last_time
Definition: rtp.h:268
gint16 v_seq_offset
Definition: rtp.h:264
uint16_t a_base_seq_prev
Definition: rtp.h:259
uint16_t v_last_seq
Definition: rtp.h:260
gint64 v_start_time
Definition: rtp.h:268
uint16_t v_prev_seq
Definition: rtp.h:260
gint64 a_start_time
Definition: rtp.h:267
gint32 a_prev_delay
Definition: rtp.h:265
uint32_t v_last_ts
Definition: rtp.h:258
gint64 a_last_time
Definition: rtp.h:267
gint16 a_seq_offset
Definition: rtp.h:263
uint32_t a_last_ts
Definition: rtp.h:257
gint32 a_active_delay
Definition: rtp.h:265
uint32_t v_target_ts
Definition: rtp.h:258
uint16_t a_prev_seq
Definition: rtp.h:259
gint64 v_evaluating_start_time
Definition: rtp.h:268
uint16_t a_last_seq
Definition: rtp.h:259
uint32_t v_prev_ts
Definition: rtp.h:258
gint64 v_reference_time
Definition: rtp.h:268
uint32_t v_base_ts_prev
Definition: rtp.h:258
gint32 a_ts_offset
Definition: rtp.h:265
uint32_t v_last_ssrc
Definition: rtp.h:258
RTP Header (http://tools.ietf.org/html/rfc3550#section-5.1)
Definition: rtp.h:36
uint16_t extension
Definition: rtp.h:40
uint32_t timestamp
Definition: rtp.h:53
uint16_t padding
Definition: rtp.h:39
uint32_t csrc[16]
Definition: rtp.h:55
uint16_t csrccount
Definition: rtp.h:41
uint16_t type
Definition: rtp.h:43
uint32_t ssrc
Definition: rtp.h:54
uint16_t seq_number
Definition: rtp.h:52
uint16_t version
Definition: rtp.h:38
uint16_t markerbit
Definition: rtp.h:42