1 /*
2  * The oRTP library is an RTP (Realtime Transport Protocol - rfc3550) implementation with additional features.
3  * Copyright (C) 2017 Belledonne Communications SARL
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19 
20 
21 #ifndef RTP_H
22 #define RTP_H
23 
24 #include <ortp/port.h>
25 #include <ortp/str_utils.h>
26 
27 #define IPMAXLEN 20
28 #define UDP_MAX_SIZE 1500
29 #define RTP_FIXED_HEADER_SIZE 12
30 #define RTP_DEFAULT_JITTER_TIME 80	/*miliseconds*/
31 #define RTP_DEFAULT_MULTICAST_TTL 5	/*hops*/
32 #define RTP_DEFAULT_MULTICAST_LOOPBACK 0  /*false*/
33 #define RTP_DEFAULT_DSCP 0x00  /*best effort*/
34 
35 
36 
37 typedef struct rtp_header
38 {
39 #ifdef ORTP_BIGENDIAN
40 	uint16_t version:2;
41 	uint16_t padbit:1;
42 	uint16_t extbit:1;
43 	uint16_t cc:4;
44 	uint16_t markbit:1;
45 	uint16_t paytype:7;
46 #else
47 	uint16_t cc:4;
48 	uint16_t extbit:1;
49 	uint16_t padbit:1;
50 	uint16_t version:2;
51 	uint16_t paytype:7;
52 	uint16_t markbit:1;
53 #endif
54 	uint16_t seq_number;
55 	uint32_t timestamp;
56 	uint32_t ssrc;
57 	uint32_t csrc[16];
58 } rtp_header_t;
59 
60 
61 
62 
63 typedef struct rtp_stats
64 {
65 	uint64_t packet_sent;		/*number of outgoing packets */
66 	uint64_t packet_dup_sent;	/*number of outgoing duplicate packets */
67 	uint64_t sent;				/* outgoing total bytes (excluding IP header) */
68 	uint64_t packet_recv;		/* number of incoming packets */
69 	uint64_t packet_dup_recv;	/* number of incoming duplicate packets */
70 	uint64_t recv;				/* incoming bytes of payload and delivered in time to the application */
71 	uint64_t hw_recv;			/* incoming bytes of payload */
72 	uint64_t outoftime;			/* number of incoming packets that were received too late */
73 	int64_t  cum_packet_loss;	/* cumulative number of incoming packet lost */
74 	uint64_t bad;				/* incoming packets that did not appear to be RTP */
75 	uint64_t discarded;			/* incoming packets discarded because the queue exceeds its max size */
76 	uint64_t sent_rtcp_packets;	/* outgoing RTCP packets counter (only packets that embed a report block are considered) */
77 	uint64_t recv_rtcp_packets;	/* incoming RTCP packets counter (only packets that embed a report block are considered) */
78 } rtp_stats_t;
79 
80 
81 typedef struct jitter_stats
82 {
83 	uint32_t jitter;			/* interarrival jitter at last emitted sender report */
84 	uint32_t max_jitter;		/* biggest interarrival jitter (value in stream clock unit) */
85 	uint64_t sum_jitter;		/* sum of all interarrival jitter (value in stream clock unit) */
86 	uint64_t max_jitter_ts;		/* date (in ms since Epoch) of the biggest interarrival jitter */
87 	float jitter_buffer_size_ms;/* mean jitter buffer size in milliseconds.*/
88 } jitter_stats_t;
89 
90 #define RTP_TIMESTAMP_IS_NEWER_THAN(ts1,ts2) \
91 	((uint32_t)((uint32_t)(ts1) - (uint32_t)(ts2))< (uint32_t)(1<<31))
92 
93 #define RTP_TIMESTAMP_IS_STRICTLY_NEWER_THAN(ts1,ts2) \
94 	( ((uint32_t)((uint32_t)(ts1) - (uint32_t)(ts2))< (uint32_t)(1<<31)) && (ts1)!=(ts2) )
95 
96 #define RTP_SEQ_IS_STRICTLY_GREATER_THAN(seq1,seq2)\
97 	(((uint16_t)((uint16_t)(seq1) - (uint16_t)(seq2))< (uint16_t)(1<<15)) && (seq1!=seq2))
98 
99 #define TIME_IS_NEWER_THAN(t1,t2) RTP_TIMESTAMP_IS_NEWER_THAN(t1,t2)
100 
101 #define TIME_IS_STRICTLY_NEWER_THAN(t1,t2) RTP_TIMESTAMP_IS_STRICTLY_NEWER_THAN(t1,t2)
102 
103 
104 #ifdef __cplusplus
105 extern "C"{
106 #endif
107 
108 /* packet api */
109 /* the first argument is a mblk_t. The header is supposed to be not splitted  */
110 #define rtp_set_markbit(mp,value)		((rtp_header_t*)((mp)->b_rptr))->markbit=(value)
111 #define rtp_set_seqnumber(mp,seq)	((rtp_header_t*)((mp)->b_rptr))->seq_number=(seq)
112 #define rtp_set_timestamp(mp,ts)	((rtp_header_t*)((mp)->b_rptr))->timestamp=(ts)
113 #define rtp_set_ssrc(mp,_ssrc)		((rtp_header_t*)((mp)->b_rptr))->ssrc=(_ssrc)
114 ORTP_PUBLIC void rtp_add_csrc(mblk_t *mp ,uint32_t csrc);
115 #define rtp_set_payload_type(mp,pt)	((rtp_header_t*)((mp)->b_rptr))->paytype=(pt)
116 
117 #define rtp_get_version(mp)	(((rtp_header_t*)((mp)->b_rptr))->version)
118 #define rtp_get_markbit(mp)	(((rtp_header_t*)((mp)->b_rptr))->markbit)
119 #define rtp_get_extbit(mp)	(((rtp_header_t*)((mp)->b_rptr))->extbit)
120 #define rtp_get_timestamp(mp)	(((rtp_header_t*)((mp)->b_rptr))->timestamp)
121 #define rtp_get_seqnumber(mp)	(((rtp_header_t*)((mp)->b_rptr))->seq_number)
122 #define rtp_get_payload_type(mp)	(((rtp_header_t*)((mp)->b_rptr))->paytype)
123 #define rtp_get_ssrc(mp)		(((rtp_header_t*)((mp)->b_rptr))->ssrc)
124 #define rtp_get_cc(mp)		(((rtp_header_t*)((mp)->b_rptr))->cc)
125 #define rtp_get_csrc(mp, idx)		(((rtp_header_t*)((mp)->b_rptr))->csrc[idx])
126 
127 ORTP_PUBLIC int rtp_get_payload(mblk_t *packet, unsigned char **start);
128 ORTP_PUBLIC int rtp_get_extheader(mblk_t *packet, uint16_t *profile, uint8_t **start_ext);
129 
130 #ifdef __cplusplus
131 }
132 #endif
133 
134 #endif
135