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 #define RTP_HEADER_SIZE 12
31 
33 typedef struct rtp_header
34 {
35 #if __BYTE_ORDER == __BIG_ENDIAN
36  uint16_t version:2;
37  uint16_t padding:1;
38  uint16_t extension:1;
39  uint16_t csrccount:4;
40  uint16_t markerbit:1;
41  uint16_t type:7;
42 #elif __BYTE_ORDER == __LITTLE_ENDIAN
43  uint16_t csrccount:4;
44  uint16_t extension:1;
45  uint16_t padding:1;
46  uint16_t version:2;
47  uint16_t type:7;
48  uint16_t markerbit:1;
49 #endif
50  uint16_t seq_number;
51  uint32_t timestamp;
52  uint32_t ssrc;
53  uint32_t csrc[16];
56 
58 typedef struct janus_rtp_packet {
59  char *data;
60  gint length;
61  gint64 created;
64 
67  uint16_t type;
68  uint16_t length;
70 
72 #define JANUS_RTP_EXTMAP_AUDIO_LEVEL "urn:ietf:params:rtp-hdrext:ssrc-audio-level"
73 
74 #define JANUS_RTP_EXTMAP_TOFFSET "urn:ietf:params:rtp-hdrext:toffset"
75 
76 #define JANUS_RTP_EXTMAP_ABS_SEND_TIME "http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time"
77 
78 #define JANUS_RTP_EXTMAP_VIDEO_ORIENTATION "urn:3gpp:video-orientation"
79 
80 #define JANUS_RTP_EXTMAP_TRANSPORT_WIDE_CC "http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01"
81 
82 #define JANUS_RTP_EXTMAP_PLAYOUT_DELAY "http://www.webrtc.org/experiments/rtp-hdrext/playout-delay"
83 
84 #define JANUS_RTP_EXTMAP_MID "urn:ietf:params:rtp-hdrext:sdes:mid"
85 
86 #define JANUS_RTP_EXTMAP_RID "urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id"
87 
88 #define JANUS_RTP_EXTMAP_REPAIRED_RID "urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id"
89 
90 #define JANUS_RTP_EXTMAP_FRAME_MARKING "http://tools.ietf.org/html/draft-ietf-avtext-framemarking-07"
91 
92 #define JANUS_RTP_EXTMAP_ENCRYPTED "urn:ietf:params:rtp-hdrext:encrypt"
93 int janus_rtp_extension_id(const char *type);
94 
95 
96 typedef enum janus_audiocodec {
106 const char *janus_audiocodec_name(janus_audiocodec acodec);
109 
110 typedef enum janus_videocodec {
118 const char *janus_videocodec_name(janus_videocodec vcodec);
121 
122 
126 gboolean janus_is_rtp(char *buf, guint len);
127 
133 char *janus_rtp_payload(char *buf, int len, int *plen);
134 
139 int janus_rtp_header_extension_get_id(const char *sdp, const char *extension);
140 
146 const char *janus_rtp_header_extension_get_from_id(const char *sdp, int id);
147 
157 int janus_rtp_header_extension_parse_audio_level(char *buf, int len, int id, gboolean *vad, int *level);
158 
168 int janus_rtp_header_extension_parse_video_orientation(char *buf, int len, int id,
169  gboolean *c, gboolean *f, gboolean *r1, gboolean *r0);
170 
178 int janus_rtp_header_extension_parse_playout_delay(char *buf, int len, int id,
179  uint16_t *min_delay, uint16_t *max_delay);
180 
188 int janus_rtp_header_extension_parse_mid(char *buf, int len, int id,
189  char *sdes_item, int sdes_len);
190 
198 int janus_rtp_header_extension_parse_rid(char *buf, int len, int id,
199  char *sdes_item, int sdes_len);
200 
209 int janus_rtp_header_extension_parse_framemarking(char *buf, int len, int id, janus_videocodec codec, uint8_t *tid);
210 
217 int janus_rtp_header_extension_parse_transport_wide_cc(char *buf, int len, int id, uint16_t *transSeqNum);
218 
225 int janus_rtp_header_extension_set_transport_wide_cc(char *buf, int len, int id, uint16_t transSeqNum);
226 
234 int janus_rtp_header_extension_replace_id(char *buf, int len, int id, int new_id);
235 
240  gboolean seq_reset, new_ssrc;
241  gint16 seq_offset;
245 
249 
255 void janus_rtp_header_update(janus_rtp_header *header, janus_rtp_switching_context *context, gboolean video, int step);
256 
257 #define RTP_AUDIO_SKEW_TH_MS 120
258 #define RTP_VIDEO_SKEW_TH_MS 120
259 #define SKEW_DETECTION_WAIT_TIME_SECS 10
260 
273 
274 
290  guint32 drop_trigger;
292  gint64 last_relayed;
298  gboolean need_pli;
300 
304 
312 void janus_rtp_simulcasting_prepare(json_t *simulcast, int *rid_ext_id, int *framemarking_ext_id, uint32_t *ssrcs, char **rids);
313 
326  char *buf, int len, uint32_t *ssrcs, char **rids,
328 
329 #endif
janus_rtp_simulcasting_context
Helper struct for processing and tracking simulcast streams.
Definition: rtp.h:276
janus_rtp_switching_context::base_seq_prev
uint16_t base_seq_prev
Definition: rtp.h:239
janus_rtp_switching_context::last_ts
uint32_t last_ts
Definition: rtp.h:238
janus_rtp_header_extension_get_id
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
janus_rtp_simulcasting_context::changed_substream
gboolean changed_substream
Whether the substream has changed after processing a packet.
Definition: rtp.h:294
JANUS_VIDEOCODEC_H265
@ JANUS_VIDEOCODEC_H265
Definition: rtp.h:116
rtp_header::extension
uint16_t extension
Definition: rtp.h:38
janus_rtp_switching_context::reference_time
gint64 reference_time
Definition: rtp.h:243
janus_audiocodec_from_name
janus_audiocodec janus_audiocodec_from_name(const char *name)
Definition: rtp.c:804
janus_rtp_header_extension::length
uint16_t length
Definition: rtp.h:68
rtp_header::csrc
uint32_t csrc[16]
Definition: rtp.h:53
janus_rtp_switching_context::base_seq
uint16_t base_seq
Definition: rtp.h:239
janus_rtp_simulcasting_context::substream_target_temp
int substream_target_temp
Definition: rtp.h:284
rtp_header::seq_number
uint16_t seq_number
Definition: rtp.h:50
janus_rtp_switching_context::base_ts_prev
uint32_t base_ts_prev
Definition: rtp.h:238
janus_rtp_header_extension_parse_audio_level
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:177
janus_rtp_simulcasting_context::substream
int substream
Which simulcast substream we should forward back.
Definition: rtp.h:282
JANUS_AUDIOCODEC_G722
@ JANUS_AUDIOCODEC_G722
Definition: rtp.h:102
janus_rtp_switching_context::start_ts
uint32_t start_ts
Definition: rtp.h:238
janus_rtp_simulcasting_context::changed_temporal
gboolean changed_temporal
Whether the temporal layer has changed after processing a packet.
Definition: rtp.h:296
rtp_header::ssrc
uint32_t ssrc
Definition: rtp.h:52
janus_rtp_simulcasting_context_reset
void janus_rtp_simulcasting_context_reset(janus_rtp_simulcasting_context *context)
Set (or reset) the context fields to their default values.
Definition: rtp.c:903
JANUS_AUDIOCODEC_PCMU
@ JANUS_AUDIOCODEC_PCMU
Definition: rtp.h:100
janus_videocodec
janus_videocodec
Definition: rtp.h:110
janus_audiocodec_name
const char * janus_audiocodec_name(janus_audiocodec acodec)
Definition: rtp.c:781
janus_rtp_packet
RTP packet.
Definition: rtp.h:58
janus_rtp_header_extension
RTP extension.
Definition: rtp.h:66
janus_rtp_simulcasting_context
struct janus_rtp_simulcasting_context janus_rtp_simulcasting_context
Helper struct for processing and tracking simulcast streams.
janus_rtp_simulcasting_context::templayer_target
int templayer_target
As above, but to handle transitions (e.g., wait for keyframe)
Definition: rtp.h:288
janus_rtp_switching_context
struct janus_rtp_switching_context janus_rtp_switching_context
RTP context, in order to make sure SSRC changes result in coherent seq/ts increases.
janus_videocodec_name
const char * janus_videocodec_name(janus_videocodec vcodec)
Definition: rtp.c:848
janus_rtp_switching_context::new_ssrc
gboolean new_ssrc
Definition: rtp.h:240
janus_rtp_payload
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
JANUS_AUDIOCODEC_ISAC_32K
@ JANUS_AUDIOCODEC_ISAC_32K
Definition: rtp.h:103
JANUS_VIDEOCODEC_VP9
@ JANUS_VIDEOCODEC_VP9
Definition: rtp.h:113
JANUS_VIDEOCODEC_AV1
@ JANUS_VIDEOCODEC_AV1
Definition: rtp.h:115
JANUS_VIDEOCODEC_VP8
@ JANUS_VIDEOCODEC_VP8
Definition: rtp.h:112
janus_rtp_switching_context_reset
void janus_rtp_switching_context_reset(janus_rtp_switching_context *context)
Set (or reset) the context fields to their default values.
Definition: rtp.c:397
janus_rtp_header_extension::type
uint16_t type
Definition: rtp.h:67
JANUS_AUDIOCODEC_NONE
@ JANUS_AUDIOCODEC_NONE
Definition: rtp.h:97
JANUS_AUDIOCODEC_MULTIOPUS
@ JANUS_AUDIOCODEC_MULTIOPUS
Definition: rtp.h:99
janus_rtp_header_extension_parse_transport_wide_cc
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:293
janus_rtp_packet::created
gint64 created
Definition: rtp.h:61
rtp_header::csrccount
uint16_t csrccount
Definition: rtp.h:39
janus_audiocodec
janus_audiocodec
Definition: rtp.h:96
janus_rtp_switching_context::last_seq
uint16_t last_seq
Definition: rtp.h:239
janus_rtp_switching_context::last_ssrc
uint32_t last_ssrc
Definition: rtp.h:238
janus_rtp_header_update
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:634
janus_rtp_switching_context::last_time
gint64 last_time
Definition: rtp.h:243
rtp_header::padding
uint16_t padding
Definition: rtp.h:37
janus_rtp_simulcasting_context_process_rtp
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:947
janus_rtp_switching_context::seq_reset
gboolean seq_reset
Definition: rtp.h:240
janus_rtp_packet::length
gint length
Definition: rtp.h:60
JANUS_AUDIOCODEC_ISAC_16K
@ JANUS_AUDIOCODEC_ISAC_16K
Definition: rtp.h:104
janus_rtp_simulcasting_context::last_relayed
gint64 last_relayed
When we relayed the last packet (used to detect when substreams become unavailable)
Definition: rtp.h:292
janus_rtp_simulcasting_context::rid_ext_id
gint rid_ext_id
RTP Stream extension ID, if any.
Definition: rtp.h:278
janus_rtp_switching_context::prev_seq
uint16_t prev_seq
Definition: rtp.h:239
janus_rtp_header_extension_parse_rid
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:251
janus_rtp_header_extension
struct janus_rtp_header_extension janus_rtp_header_extension
RTP extension.
janus_rtp_switching_context::prev_delay
gint32 prev_delay
Definition: rtp.h:242
janus_rtp_simulcasting_context::templayer
int templayer
Which simulcast temporal layer we should forward back.
Definition: rtp.h:286
janus_videocodec_from_name
janus_videocodec janus_videocodec_from_name(const char *name)
Definition: rtp.c:867
rtp_header
struct rtp_header rtp_header
RTP Header (http://tools.ietf.org/html/rfc3550#section-5.1)
janus_rtp_switching_context
RTP context, in order to make sure SSRC changes result in coherent seq/ts increases.
Definition: rtp.h:237
janus_rtp_switching_context::base_ts
uint32_t base_ts
Definition: rtp.h:238
janus_rtp_header_extension_set_transport_wide_cc
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:313
json_t
struct json_t json_t
Definition: plugin.h:236
janus_rtp_skew_compensate_audio
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:404
janus_rtp_header
rtp_header janus_rtp_header
Definition: rtp.h:55
janus_rtp_switching_context::prev_ts
uint32_t prev_ts
Definition: rtp.h:238
janus_rtp_simulcasting_context::drop_trigger
guint32 drop_trigger
How much time (in us, default 250000) without receiving packets will make us drop to the substream be...
Definition: rtp.h:290
janus_rtp_header_extension_parse_playout_delay
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:214
rtp_header::type
uint16_t type
Definition: rtp.h:41
janus_is_rtp
gboolean janus_is_rtp(char *buf, guint len)
Helper method to demultiplex RTP from other protocols.
Definition: rtp.c:19
janus_rtp_simulcasting_context::need_pli
gboolean need_pli
Whether we need to send the user a keyframe request (PLI)
Definition: rtp.h:298
janus_rtp_switching_context::start_time
gint64 start_time
Definition: rtp.h:243
janus_rtp_switching_context::evaluating_start_time
gint64 evaluating_start_time
Definition: rtp.h:243
rtp_header::version
uint16_t version
Definition: rtp.h:36
janus_rtp_simulcasting_context::framemarking_ext_id
gint framemarking_ext_id
Frame marking extension ID, if any.
Definition: rtp.h:280
JANUS_VIDEOCODEC_NONE
@ JANUS_VIDEOCODEC_NONE
Definition: rtp.h:111
janus_rtp_switching_context::ts_offset
gint32 ts_offset
Definition: rtp.h:242
janus_rtp_header_extension_parse_framemarking
int janus_rtp_header_extension_parse_framemarking(char *buf, int len, int id, janus_videocodec codec, uint8_t *tid)
Helper to parse a frame-marking RTP extension (http://tools.ietf.org/html/draft-ietf-avtext-framemark...
Definition: rtp.c:273
janus_rtp_header_extension_replace_id
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:327
janus_rtp_packet::data
char * data
Definition: rtp.h:59
janus_rtp_switching_context::seq_offset
gint16 seq_offset
Definition: rtp.h:241
rtp_header::markerbit
uint16_t markerbit
Definition: rtp.h:40
janus_rtp_packet::last_retransmit
gint64 last_retransmit
Definition: rtp.h:62
janus_rtp_simulcasting_prepare
void janus_rtp_simulcasting_prepare(json_t *simulcast, int *rid_ext_id, int *framemarking_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:914
JANUS_VIDEOCODEC_H264
@ JANUS_VIDEOCODEC_H264
Definition: rtp.h:114
janus_rtp_header_extension_parse_video_orientation
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:192
JANUS_AUDIOCODEC_PCMA
@ JANUS_AUDIOCODEC_PCMA
Definition: rtp.h:101
janus_audiocodec_pt
int janus_audiocodec_pt(janus_audiocodec acodec)
Definition: rtp.c:824
janus_rtp_extension_id
int janus_rtp_extension_id(const char *type)
Definition: rtp.c:370
janus_rtp_packet
struct janus_rtp_packet janus_rtp_packet
RTP packet.
janus_rtp_header_extension_parse_mid
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:230
janus_rtp_simulcasting_context::substream_target
int substream_target
As above, but to handle transitions (e.g., wait for keyframe, or get this if available)
Definition: rtp.h:284
janus_rtp_switching_context::target_ts
uint32_t target_ts
Definition: rtp.h:238
JANUS_AUDIOCODEC_OPUS
@ JANUS_AUDIOCODEC_OPUS
Definition: rtp.h:98
janus_rtp_skew_compensate_video
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:520
janus_rtp_switching_context::active_delay
gint32 active_delay
Definition: rtp.h:242
rtp_header
RTP Header (http://tools.ietf.org/html/rfc3550#section-5.1)
Definition: rtp.h:34
rtp_header::timestamp
uint32_t timestamp
Definition: rtp.h:51
janus_videocodec_pt
int janus_videocodec_pt(janus_videocodec vcodec)
Definition: rtp.c:883
janus_rtp_header_extension_get_from_id
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:80