rtp.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. #ifndef __RTP_H__
  2. #define __RTP_H__
  3. #pragma once
  4. #include "config.h"
  5. #include <winpr/wtypes.h>
  6. #ifndef _WIN32
  7. #include <sys/time.h>
  8. #endif
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. #define IP_UDP_OVERHEAD (20 + 8)
  13. #define RTP_VERSION 2
  14. #define RTP_MAX_SDES 255 /* maximum text length for SDES */
  15. /** Standard RTP static payload types, as defined by RFC 3551. */
  16. enum rtp_pt
  17. {
  18. RTP_PT_PCMU = 0, /**< audio PCMU */
  19. RTP_PT_G726_32 = 2, /**< audio G726-32 */
  20. RTP_PT_GSM = 3, /**< audio GSM */
  21. RTP_PT_G723 = 4, /**< audio G723 */
  22. RTP_PT_DVI4_8K = 5, /**< audio DVI4 8KHz */
  23. RTP_PT_DVI4_16K = 6, /**< audio DVI4 16Khz */
  24. RTP_PT_LPC = 7, /**< audio LPC */
  25. RTP_PT_PCMA = 8, /**< audio PCMA */
  26. RTP_PT_G722 = 9, /**< audio G722 */
  27. RTP_PT_L16_2 = 10, /**< audio 16bit linear 44.1KHz stereo */
  28. RTP_PT_L16_1 = 11, /**< audio 16bit linear 44.1KHz mono */
  29. RTP_PT_QCELP = 12, /**< audio QCELP */
  30. RTP_PT_CN = 13, /**< audio Comfort Noise */
  31. RTP_PT_MPA = 14, /**< audio MPEG1/MPEG2 elemetr. streams */
  32. RTP_PT_G728 = 15, /**< audio G728 */
  33. RTP_PT_DVI4_11K = 16, /**< audio DVI4 11.025KHz mono */
  34. RTP_PT_DVI4_22K = 17, /**< audio DVI4 22.050KHz mono */
  35. RTP_PT_G729 = 18, /**< audio G729 */
  36. RTP_PT_CELB = 25, /**< video/comb Cell-B by Sun (RFC2029) */
  37. RTP_PT_JPEG = 26, /**< video JPEG */
  38. RTP_PT_NV = 28, /**< video NV by nv program by Xerox */
  39. RTP_PT_H261 = 31, /**< video H261 */
  40. RTP_PT_MPV = 32, /**< video MPEG1 or MPEG2 elementary */
  41. RTP_PT_MP2T = 33, /**< video MPEG2 transport */
  42. RTP_PT_H263 = 34, /**< video H263 */
  43. RTP_PT_DYNAMIC = 96, /**< start of dynamic RTP payload */
  44. RTP_PT_PCM8 = 102,
  45. RTP_PT_PCM16 = 103,
  46. RTP_PT_BV16 = 106,
  47. RTP_PT_H263P = 107,
  48. RTP_PT_H264 = 108,
  49. RTP_PT_MPG4 = 109,
  50. RTP_PT_VP8 = 110,
  51. RTP_PT_BV32 = 127,
  52. };
  53. /* rtp data header */
  54. /*
  55. 0 1 2 3
  56. 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
  57. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  58. |V=2|P|X| CC |M| PT | sequence number |
  59. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  60. | timestamp |
  61. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  62. | synchronization source (SSRC) identifier |
  63. +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
  64. | contributing source (CSRC) identifiers |
  65. | .... |
  66. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  67. */
  68. #pragma pack(push, 2)
  69. typedef struct rtp_hdr {
  70. unsigned short csrc : 4; /* CSRC identifier count */
  71. unsigned short x : 1; /* extension */
  72. unsigned short p : 1; /* padding */
  73. unsigned short v : 2; /* version */
  74. unsigned short pt : 7; /* payload type */
  75. unsigned short m : 1; /* marker */
  76. unsigned short seq; /* sequence number */
  77. unsigned int ts; /* time stamp */
  78. unsigned int ssrc; /* synchronization source (SSRC) id */
  79. } rtp_hdr;
  80. #pragma pack(pop)
  81. typedef enum {
  82. RTCP_SR = 200,
  83. RTCP_RR = 201,
  84. RTCP_SDES = 202,
  85. RTCP_BYE = 203,
  86. RTCP_APP = 204,
  87. RTCP_FIR = 192, /* h261 specific, HW use this to request full intra-frame */
  88. RTCP_NACK = 193, /* h261 specific */
  89. RTCP_RTPFB = 205,
  90. RTCP_PSFB = 206,
  91. RTCP_XR = 207
  92. } rtcp_type_t;
  93. typedef enum {
  94. RTCP_SDES_END = 0,
  95. RTCP_SDES_CNAME = 1,
  96. RTCP_SDES_NAME = 2,
  97. RTCP_SDES_EMAIL = 3,
  98. RTCP_SDES_PHONE = 4,
  99. RTCP_SDES_LOC = 5,
  100. RTCP_SDES_TOOL = 6,
  101. RTCP_SDES_NOTE = 7,
  102. RTCP_SDES_PRIV = 8,
  103. RTCP_SDES_COUNT = 9
  104. } rtcp_sdes_type_t;
  105. /*
  106. 0 1 2 3
  107. 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
  108. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  109. |V=2|P| MBZ | PT=RTCP_FIR | length |
  110. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  111. | SSRC |
  112. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  113. Full INTRA-frame Request (FIR) packet for Hua-wei Open-eye compatibility
  114. 0 1 2 3
  115. 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
  116. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  117. |V=2|P| MBZ | PT=RTCP_NACK | length |
  118. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  119. | SSRC |
  120. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  121. | FSN | BLP |
  122. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  123. Negative ACKnowledgements (NACK) packet
  124. */
  125. /*
  126. * RTCP common header word
  127. */
  128. typedef struct {
  129. unsigned short count:5; /* varies by packet type */
  130. unsigned short p:1; /* padding flag */
  131. unsigned short version:2; /* protocol version */
  132. unsigned short pt:8; /* RTCP packet type */
  133. unsigned short length; /* pkt len in words, w/o this word */
  134. } rtcp_common_t;
  135. /* sender info */
  136. typedef struct {
  137. unsigned int ssrc; /* sender generating this report */
  138. unsigned int ntp_sec; /* NTP timestamp */
  139. unsigned int ntp_frac;
  140. unsigned int rtp_ts; /* RTP timestamp */
  141. unsigned int psent; /* packets sent */
  142. unsigned int osent; /* octets sent */
  143. }rtcp_sender_info_t;
  144. /*
  145. * Reception report block
  146. */
  147. typedef struct {
  148. unsigned int ssrc; /* data source being reported */
  149. unsigned int fl_cnpl; /* fraction lost since last SR/RR, cumul. no. pkts lost (signed!) */
  150. unsigned int last_seq; /* extended last seq. no. received */
  151. unsigned int jitter; /* interarrival jitter */
  152. unsigned int lsr; /* last SR packet from this source */
  153. unsigned int dlsr; /* delay since last SR packet */
  154. } rtcp_rr_t;
  155. /*
  156. * SDES item
  157. */
  158. #pragma pack(push, 1)
  159. typedef struct {
  160. unsigned char type; /* type of item (rtcp_sdes_type_t) */
  161. unsigned char length; /* length of item (in octets) */
  162. char data[1]; /* text, not null-terminated */
  163. } rtcp_sdes_item_t;
  164. #pragma pack(pop)
  165. typedef struct {
  166. unsigned int ssrc;
  167. rtcp_sdes_item_t item[1]; /* list of SDES items */
  168. }rtcp_sdes_t;
  169. /* BYE */
  170. #pragma pack(push, 1)
  171. typedef struct {
  172. unsigned char length;
  173. char reason[1];
  174. } rtcp_bye_reason_t;
  175. #pragma pack(pop)
  176. typedef struct {
  177. rtcp_common_t common;
  178. unsigned int ssrc[1]; /* the bye may contain several ssrc/csrc */
  179. } rtcp_bye_t;
  180. typedef struct {
  181. rtcp_common_t common;
  182. //huchen delete, sender ssrc in sender_info struct.
  183. //unsigned int ssrc;
  184. rtcp_sender_info_t sender_info;
  185. rtcp_rr_t rb[1];
  186. } rtcp_sr_t;
  187. typedef struct {
  188. rtcp_common_t common;
  189. unsigned int ssrc;
  190. char name[4];
  191. } rtcp_app_t;
  192. typedef struct {
  193. rtcp_common_t common;
  194. unsigned int ssrc;
  195. }rtcp_fir_t;
  196. /* RTCP FB packet */
  197. typedef enum {
  198. RTCP_RTPFB_NACK = 1,
  199. RTCP_RTPFB_TMMBR = 3,
  200. RTCP_RTPFB_TMMBN = 4
  201. } rtcp_rtpfb_type_t;
  202. typedef struct _rtcp_fb_header {
  203. unsigned int packet_sender_ssrc;
  204. unsigned int media_source_ssrc;
  205. } rtcp_fb_header_t;
  206. typedef struct rtcp_fb_tmmbr_fci {
  207. unsigned int ssrc;
  208. unsigned int value;
  209. } rtcp_fb_tmmbr_fci_t;
  210. typedef struct _RtcpTmmbrInfo {
  211. char *sent;
  212. char *received;
  213. } RtcpTmmbrInfo;
  214. typedef struct {
  215. unsigned int peer_ssrc; /* network byte order */
  216. unsigned int total_rx_bytes;
  217. unsigned int total_tx_bytes;
  218. unsigned int total_rx_packets;
  219. unsigned int total_tx_packets;
  220. unsigned int total_packet_lost;
  221. unsigned int rx_packets_since_last_sr;
  222. unsigned int rx_seq_at_last_sr;
  223. unsigned short first_seq; /* the first seq we recv */
  224. unsigned short max_seq;
  225. unsigned short last_seq; /* last seq */
  226. unsigned short seq_circles;
  227. unsigned int inter_jitter; /* interarrival jitter */
  228. unsigned int transit;
  229. unsigned int lsr; /* last sr ntp */
  230. struct timeval lsr_tm; /* local time ticks after receive */
  231. float rtt;/*last round trip delay calculated*/
  232. int cum_loss;
  233. unsigned int fraction_lost;
  234. unsigned int report_block_last_seq;
  235. int report_block_last_number_of_packets;
  236. u__int64_t tmmbr_max_bitrate;
  237. }rtcp_statistics;
  238. #pragma pack(1)
  239. /**
  240. * Declaration for DTMF telephony-events (RFC2833).
  241. */
  242. struct rtp_dtmf_event
  243. {
  244. unsigned char event; /**< Event type ID. */
  245. unsigned char e_vol : 6; /**< Event volume. */
  246. unsigned char p : 1; /* reserved */
  247. unsigned char e : 1; /* end bit */
  248. unsigned short duration; /**< Event duration. */
  249. };
  250. typedef struct rtp_dtmf_event rtp_dtmf_event;
  251. #pragma pack()
  252. /* rtp state */
  253. typedef struct rtp_state rtp_state;
  254. int rtp_state_get_tmmbr_wait_send_maxbitrate(rtp_state *rs, u__int64_t *mxtbr);
  255. TOOLKIT_API int rtp_state_fill_rtp(rtp_state *rs, void *hdr, unsigned int pt, unsigned int mark, unsigned int delta_ts);
  256. TOOLKIT_API int rtp_state_advance_timestamp(rtp_state *rs, unsigned int delta_ts);
  257. TOOLKIT_API int rtp_state_set_rtcp_sdes_string(rtp_state *rs,
  258. rtcp_sdes_type_t type,
  259. const char *str);
  260. TOOLKIT_API int rtp_state_rtcp_make_sr(rtp_state *rs, char *buf, size_t buflen);
  261. TOOLKIT_API int rtp_state_rtcp_make_rr(rtp_state *rs, char *buf, size_t buflen);
  262. TOOLKIT_API int rtp_state_rtcp_make_h261_fir(rtp_state *rs, char *buf, size_t buflen);
  263. TOOLKIT_API int rtp_state_rtcp_make_rtcp_fb_tmmbn(rtp_state *rs, char *buf, size_t buflen, unsigned int ssrc);
  264. TOOLKIT_API int rtp_state_rtcp_make_rtcp_fb_tmmbr(rtp_state *rs, char *buf, size_t buflen, u__int64_t mxtbr, unsigned short measured_overhead);
  265. /**
  266. * called when receive a peer's rtp data packet
  267. */
  268. TOOLKIT_API int rtp_state_on_recv_rtcp(rtp_state *rs, const void *buf, int len);
  269. /**
  270. * called when receive a peer's rtp data packet
  271. */
  272. TOOLKIT_API int rtp_state_on_recv_rtp(rtp_state *rs, const void *buf, int len);
  273. /**
  274. * called when send rtp data packet out
  275. */
  276. TOOLKIT_API int rtp_state_on_send_rtp(rtp_state *rs, int len);
  277. /**
  278. * set sdes string, only ascii string less than 255 accepted
  279. */
  280. TOOLKIT_API int rtp_state_reset(rtp_state *rs, unsigned int ssrc, unsigned int padding, unsigned int extension);
  281. TOOLKIT_API rtp_state *rtp_state_create(int rtcp_transmit_interval);
  282. TOOLKIT_API void rtp_state_destroy(rtp_state *rs);
  283. TOOLKIT_API int rtp_state_get_stat(rtp_state *rs, rtcp_statistics *stat);
  284. TOOLKIT_API int rtp_state_need_send_rtcp(rtp_state *state, int update_ticks);
  285. #ifdef __cplusplus
  286. } // extern "C" {
  287. #endif
  288. #endif //__RTP_H__