rtp.h 10 KB

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