您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

1187 行
42KB

  1. #include "rtp-internal.h"
  2. #include "rtp-util.h"
  3. #include <errno.h>
  4. #define TCC01_CLOCK_RESOLUTION 250 // us
  5. static int rtcp_rtpfb_nack_pack(const rtcp_nack_t* nack, int count, uint8_t* ptr, uint32_t bytes);
  6. static int rtcp_rtpfb_tmmbr_pack(const rtcp_tmmbr_t* tmmbr, int count, uint8_t* ptr, uint32_t bytes);
  7. static int rtcp_rtpfb_tmmbn_pack(const rtcp_tmmbr_t* tmmbr, int count, uint8_t* ptr, uint32_t bytes);
  8. static int rtcp_rtpfb_tllei_pack(const rtcp_nack_t* nack, int count, uint8_t* ptr, uint32_t bytes);
  9. static int rtcp_rtpfb_ecn_pack(const rtcp_ecn_t* ecn, uint8_t* ptr, uint32_t bytes);
  10. static int rtcp_rtpfb_ps_pack(uint32_t target, uint8_t cmd, uint8_t len, uint16_t id, const uint8_t* payload, uint8_t* ptr, uint32_t bytes);
  11. static int rtcp_rtpfb_ccfb_pack(uint32_t ssrc, uint16_t begin, const rtcp_ccfb_t* ccfb, int count, uint32_t timestamp, uint8_t* ptr, uint32_t bytes);
  12. static int rtcp_rtpfb_tcc01_pack(uint16_t begin, const rtcp_ccfb_t* ccfb, int count, uint32_t timestamp, uint8_t cc, uint8_t* ptr, uint32_t bytes);
  13. static int rtcp_rtpfb_nack_unpack(struct rtp_context* ctx, const rtcp_header_t* header, struct rtcp_msg_t* msg, const uint8_t* ptr, size_t bytes);
  14. static int rtcp_rtpfb_tmmbr_unpack(struct rtp_context* ctx, const rtcp_header_t* header, struct rtcp_msg_t* msg, const uint8_t* ptr, size_t bytes);
  15. static int rtcp_rtpfb_tmmbn_unpack(struct rtp_context* ctx, const rtcp_header_t* header, struct rtcp_msg_t* msg, const uint8_t* ptr, size_t bytes);
  16. static int rtcp_rtpfb_srreq_unpack(struct rtp_context* ctx, const rtcp_header_t* header, struct rtcp_msg_t* msg, const uint8_t* ptr, size_t bytes);
  17. static int rtcp_rtpfb_rams_unpack(struct rtp_context* ctx, const rtcp_header_t* header, struct rtcp_msg_t* msg, const uint8_t* ptr, size_t bytes);
  18. static int rtcp_rtpfb_tllei_unpack(struct rtp_context* ctx, const rtcp_header_t* header, struct rtcp_msg_t* msg, const uint8_t* ptr, size_t bytes);
  19. static int rtcp_rtpfb_ecn_unpack(struct rtp_context* ctx, const rtcp_header_t* header, struct rtcp_msg_t* msg, const uint8_t* ptr, size_t bytes);
  20. static int rtcp_rtpfb_ps_unpack(struct rtp_context* ctx, const rtcp_header_t* header, struct rtcp_msg_t* msg, const uint8_t* ptr, size_t bytes);
  21. static int rtcp_rtpfb_dbi_unpack(struct rtp_context* ctx, const rtcp_header_t* header, struct rtcp_msg_t* msg, const uint8_t* ptr, size_t bytes);
  22. static int rtcp_rtpfb_ccfb_unpack(struct rtp_context* ctx, const rtcp_header_t* header, struct rtcp_msg_t* msg, const uint8_t* ptr, size_t bytes);
  23. static int rtcp_rtpfb_tcc01_unpack(struct rtp_context* ctx, const rtcp_header_t* header, struct rtcp_msg_t* msg, const uint8_t* ptr, size_t bytes);
  24. // https://datatracker.ietf.org/doc/html/rfc4585#section-6.2.1
  25. /*
  26. 0 1 2 3
  27. 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
  28. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  29. | PID | BLP |
  30. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  31. */
  32. static int rtcp_rtpfb_nack_unpack(struct rtp_context* ctx, const rtcp_header_t* header, struct rtcp_msg_t* msg, const uint8_t* ptr, size_t bytes)
  33. {
  34. uint32_t i;
  35. rtcp_nack_t* nack, nack0[32];
  36. if (bytes / 4 > sizeof(nack0) / sizeof(nack0[0]))
  37. {
  38. nack = calloc(bytes / 4, sizeof(*nack));
  39. if (!nack) return -ENOMEM;
  40. }
  41. else
  42. {
  43. nack = nack0;
  44. memset(nack, 0, sizeof(nack[0]) * (bytes / 4));
  45. }
  46. for (i = 0; i < bytes / 4; i++)
  47. {
  48. nack[i].pid = nbo_r16(ptr);
  49. nack[i].blp = nbo_r16(ptr + 2);
  50. ptr += 4;
  51. }
  52. msg->u.rtpfb.u.nack.nack = nack;
  53. msg->u.rtpfb.u.nack.count = i;
  54. ctx->handler.on_rtcp(ctx->cbparam, msg);
  55. (void)ctx, (void)header;
  56. if (nack && nack != nack0)
  57. free(nack);
  58. return 0;
  59. }
  60. static int rtcp_rtpfb_nack_pack(const rtcp_nack_t* nack, int count, uint8_t* ptr, uint32_t bytes)
  61. {
  62. int i;
  63. for (i = 0; i < count && bytes >= 4; i++)
  64. {
  65. nbo_w16(ptr, nack[i].pid);
  66. nbo_w16(ptr+2, nack[i].blp);
  67. bytes -= 4;
  68. ptr += 4;
  69. }
  70. return i * 4;
  71. }
  72. // https://www.rfc-editor.org/rfc/rfc5104.html#section-4.2.1
  73. /*
  74. 0 1 2 3
  75. 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
  76. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  77. | SSRC |
  78. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  79. | MxTBR Exp | MxTBR Mantissa |Measured Overhead|
  80. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  81. */
  82. static int rtcp_rtpfb_tmmbr_unpack(struct rtp_context* ctx, const rtcp_header_t* header, struct rtcp_msg_t* msg, const uint8_t* ptr, size_t bytes)
  83. {
  84. uint32_t i;
  85. rtcp_tmmbr_t* tmmbr, tmmbr0[4];
  86. if (bytes / 8 > sizeof(tmmbr0) / sizeof(tmmbr0[0]))
  87. {
  88. tmmbr = calloc(bytes / 8, sizeof(*tmmbr));
  89. if (!tmmbr) return -ENOMEM;
  90. }
  91. else
  92. {
  93. tmmbr = tmmbr0;
  94. memset(tmmbr, 0, sizeof(tmmbr[0]) * (bytes / 8));
  95. }
  96. for (i = 0; i < bytes / 8; i++)
  97. {
  98. tmmbr[i].ssrc = nbo_r32(ptr);
  99. tmmbr[i].exp = (ptr[4] >> 2) & 0x3F;
  100. tmmbr[i].mantissa = ((ptr[4] & 0x03) << 15) | (ptr[5] << 7) | ((ptr[6] >> 1) & 0x7F);
  101. tmmbr[i].overhead = ((ptr[6] & 0x01) << 8) | ptr[7];
  102. ptr += 8;
  103. }
  104. msg->u.rtpfb.u.tmmbr.tmmbr = tmmbr;
  105. msg->u.rtpfb.u.tmmbr.count = i;
  106. ctx->handler.on_rtcp(ctx->cbparam, msg);
  107. (void)ctx, (void)header;
  108. if (tmmbr && tmmbr != tmmbr0)
  109. free(tmmbr);
  110. return 0;
  111. }
  112. static int rtcp_rtpfb_tmmbr_pack(const rtcp_tmmbr_t* tmmbr, int count, uint8_t* ptr, uint32_t bytes)
  113. {
  114. int i;
  115. for (i = 0; i < count && bytes >= 8; i++)
  116. {
  117. nbo_w32(ptr, tmmbr[i].ssrc);
  118. nbo_w32(ptr + 4, ((tmmbr[i].exp & 0x3F) << 26) | ((tmmbr[i].mantissa & 0x1FFFF) << 9) | (tmmbr[i].overhead & 0x1FF));
  119. bytes -= 8;
  120. ptr += 8;
  121. }
  122. return i * 8;
  123. }
  124. // https://www.rfc-editor.org/rfc/rfc5104.html#section-4.2.2
  125. static int rtcp_rtpfb_tmmbn_unpack(struct rtp_context* ctx, const rtcp_header_t* header, struct rtcp_msg_t* msg, const uint8_t* ptr, size_t bytes)
  126. {
  127. uint32_t i;
  128. rtcp_tmmbr_t* tmmbr, tmmbr0[4];
  129. if (bytes / 8 > sizeof(tmmbr0) / sizeof(tmmbr0[0]))
  130. {
  131. tmmbr = calloc(bytes / 8, sizeof(*tmmbr));
  132. if (!tmmbr) return -ENOMEM;
  133. }
  134. else
  135. {
  136. tmmbr = tmmbr0;
  137. memset(tmmbr, 0, sizeof(tmmbr[0]) * (bytes / 8));
  138. }
  139. for (i = 0; i < bytes / 8; i++)
  140. {
  141. tmmbr[i].ssrc = nbo_r32(ptr);
  142. tmmbr[i].exp = (ptr[4] >> 2) & 0x3F;
  143. tmmbr[i].mantissa = ((ptr[4] & 0x03) << 15) | (ptr[5] << 7) | ((ptr[6] >> 1) & 0x7F);
  144. tmmbr[i].overhead = ((ptr[6] & 0x01) << 8) | ptr[7];
  145. ptr += 8;
  146. }
  147. msg->u.rtpfb.u.tmmbr.tmmbr = tmmbr;
  148. msg->u.rtpfb.u.tmmbr.count = i;
  149. ctx->handler.on_rtcp(ctx->cbparam, msg);
  150. (void)ctx, (void)header;
  151. if (tmmbr && tmmbr != tmmbr0)
  152. free(tmmbr);
  153. return 0;
  154. }
  155. static int rtcp_rtpfb_tmmbn_pack(const rtcp_tmmbr_t* tmmbr, int count, uint8_t* ptr, uint32_t bytes)
  156. {
  157. return rtcp_rtpfb_tmmbr_pack(tmmbr, count, ptr, bytes);
  158. }
  159. // https://www.rfc-editor.org/rfc/rfc6051.html#section-3.2
  160. static int rtcp_rtpfb_srreq_unpack(struct rtp_context* ctx, const rtcp_header_t* header, struct rtcp_msg_t* msg, const uint8_t* ptr, size_t bytes)
  161. {
  162. // The SSRC of the packet sender indicates the member that is unable to synchronise media streams,
  163. // while the SSRC of the media source indicates the sender of the media it is unable to synchronise.
  164. // The length MUST equal 2.
  165. assert(bytes == 0);
  166. (void)ctx, (void)header, (void)msg, (void)ptr;
  167. return 0;
  168. }
  169. // https://www.rfc-editor.org/rfc/rfc6285.html#section-7
  170. /*
  171. 0 1 2 3
  172. 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
  173. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  174. | Type | Reserved | Length |
  175. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  176. : Value :
  177. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  178. Figure 5: Structure of a TLV Element
  179. 0 1 2 3
  180. 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
  181. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  182. | SFMT=1 | Reserved |
  183. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  184. : Requested Media Sender SSRC(s) :
  185. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  186. : Optional TLV-encoded Fields (and Padding, if needed) :
  187. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  188. Figure 7: FCI Field Syntax for the RAMS Request Message
  189. 0 1 2 3
  190. 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
  191. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  192. | SFMT=2 | MSN | Response |
  193. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  194. : Optional TLV-encoded Fields (and Padding, if needed) :
  195. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  196. Figure 8: FCI Field Syntax for the RAMS Information Message
  197. */
  198. static int rtcp_rtpfb_rams_unpack(struct rtp_context* ctx, const rtcp_header_t* header, struct rtcp_msg_t* msg, const uint8_t* ptr, size_t bytes)
  199. {
  200. uint8_t sfmt;
  201. if (bytes < 4)
  202. return -1;
  203. sfmt = ptr[0];
  204. (void)ctx, (void)header, (void)msg;
  205. return 0;
  206. }
  207. // https://www.rfc-editor.org/rfc/rfc6642.html#section-5.1
  208. /*
  209. 0 1 2 3
  210. 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
  211. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  212. | PID | BLP |
  213. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  214. */
  215. static int rtcp_rtpfb_tllei_unpack(struct rtp_context* ctx, const rtcp_header_t* header, struct rtcp_msg_t* msg, const uint8_t* ptr, size_t bytes)
  216. {
  217. uint32_t i;
  218. rtcp_nack_t* nack, nack0[32];
  219. if (bytes / 4 > sizeof(nack0) / sizeof(nack0[0]))
  220. {
  221. nack = calloc(bytes / 4, sizeof(*nack));
  222. if (!nack) return -ENOMEM;
  223. }
  224. else
  225. {
  226. nack = nack0;
  227. memset(nack, 0, sizeof(nack[0]) * (bytes / 4));
  228. }
  229. for (i = 0; i < bytes / 4; i++)
  230. {
  231. nack[i].pid = nbo_r16(ptr);
  232. nack[i].blp = nbo_r16(ptr + 2);
  233. ptr += 4;
  234. }
  235. msg->u.rtpfb.u.nack.nack = nack;
  236. msg->u.rtpfb.u.nack.count = i;
  237. ctx->handler.on_rtcp(ctx->cbparam, msg);
  238. (void)ctx, (void)header;
  239. if (nack && nack != nack0)
  240. free(nack);
  241. return 0;
  242. }
  243. static int rtcp_rtpfb_tllei_pack(const rtcp_nack_t* nack, int count, uint8_t* ptr, uint32_t bytes)
  244. {
  245. return rtcp_rtpfb_nack_pack(nack, count, ptr, bytes);
  246. }
  247. // https://www.rfc-editor.org/rfc/rfc6679.html#section-5.1
  248. /*
  249. 0 1 2 3
  250. 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
  251. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  252. | Extended Highest Sequence Number |
  253. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  254. | ECT (0) Counter |
  255. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  256. | ECT (1) Counter |
  257. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  258. | ECN-CE Counter | not-ECT Counter |
  259. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  260. | Lost Packets Counter | Duplication Counter |
  261. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  262. */
  263. static int rtcp_rtpfb_ecn_unpack(struct rtp_context* ctx, const rtcp_header_t* header, struct rtcp_msg_t* msg, const uint8_t* ptr, size_t bytes)
  264. {
  265. rtcp_ecn_t ecn;
  266. if (bytes < 20)
  267. return -1;
  268. ecn.ext_highest_seq = nbo_r32(ptr);
  269. ecn.ect[0] = nbo_r32(ptr + 4);
  270. ecn.ect[1] = nbo_r32(ptr + 8);
  271. ecn.ect_ce_counter = nbo_r16(ptr + 12);
  272. ecn.not_ect_counter = nbo_r16(ptr + 14);
  273. ecn.lost_packets_counter = nbo_r16(ptr + 16);
  274. ecn.duplication_counter = nbo_r16(ptr + 18);
  275. memcpy(&msg->u.rtpfb.u.ecn, &ecn, sizeof(ecn));
  276. ctx->handler.on_rtcp(ctx->cbparam, msg);
  277. (void)ctx, (void)header;
  278. return 0;
  279. }
  280. static int rtcp_rtpfb_ecn_pack(const rtcp_ecn_t* ecn, uint8_t* ptr, uint32_t bytes)
  281. {
  282. if (bytes < 20)
  283. return -1;
  284. nbo_w32(ptr, ecn->ext_highest_seq);
  285. nbo_w32(ptr + 4, ecn->ect[0]);
  286. nbo_w32(ptr + 8, ecn->ect[1]);
  287. nbo_w16(ptr + 12, ecn->ect_ce_counter);
  288. nbo_w16(ptr + 14, ecn->not_ect_counter);
  289. nbo_w16(ptr + 16, ecn->lost_packets_counter);
  290. nbo_w16(ptr + 18, ecn->duplication_counter);
  291. return 20;
  292. }
  293. // https://www.rfc-editor.org/rfc/rfc7728.html#section-7
  294. /*
  295. 0 1 2 3
  296. 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
  297. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  298. | Target SSRC |
  299. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  300. | Type | Res | Parameter Len | PauseID |
  301. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  302. : Type Specific :
  303. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  304. */
  305. static int rtcp_rtpfb_ps_unpack(struct rtp_context* ctx, const rtcp_header_t* header, struct rtcp_msg_t* msg, const uint8_t* ptr, size_t bytes)
  306. {
  307. uint16_t id;
  308. uint32_t target;
  309. uint8_t cmd;
  310. uint32_t len;
  311. while(bytes >= 8)
  312. {
  313. target = nbo_r32(ptr); // target ssrc
  314. cmd = ptr[4] >> 4;
  315. len = ptr[5];
  316. id = nbo_r16(ptr + 6);
  317. if (len * 4 + 8 > bytes)
  318. {
  319. assert(0);
  320. return -1;
  321. }
  322. msg->u.rtpfb.u.ps.target = target;
  323. msg->u.rtpfb.u.ps.cmd = cmd;
  324. msg->u.rtpfb.u.ps.len = len;
  325. msg->u.rtpfb.u.ps.id = id;
  326. msg->u.rtpfb.u.ps.payload = (uint8_t*)ptr + 8;
  327. ctx->handler.on_rtcp(ctx->cbparam, msg);
  328. ptr += 8 + len * 4;
  329. bytes -= 8 + len * 4;
  330. }
  331. (void)ctx, (void)header;
  332. return 0;
  333. }
  334. static int rtcp_rtpfb_ps_pack(uint32_t target, uint8_t cmd, uint8_t len, uint16_t id, const uint8_t* payload, uint8_t* ptr, uint32_t bytes)
  335. {
  336. if (bytes < 8 + (uint32_t)len * 4)
  337. return -1;
  338. nbo_w32(ptr, target);
  339. nbo_w32(ptr + 4, ((cmd & 0x0F) << 28) | ((len & 0xFF) << 16) | id);
  340. if (len > 0)
  341. memcpy(ptr + 8, payload, len * 4);
  342. return 8 + len * 4;
  343. }
  344. // https://portal.3gpp.org/desktopmodules/Specifications/SpecificationDetails.aspx?specificationId=1404
  345. /*
  346. 0 1 2 3
  347. 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
  348. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  349. | delay |s|q| zero padding |
  350. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  351. */
  352. static int rtcp_rtpfb_dbi_unpack(struct rtp_context* ctx, const rtcp_header_t* header, struct rtcp_msg_t* msg, const uint8_t* ptr, size_t bytes)
  353. {
  354. if (bytes < 4)
  355. return -1;
  356. msg->u.rtpfb.u.dbi.delay = nbo_r16(ptr);
  357. msg->u.rtpfb.u.dbi.s = (ptr[2] >> 7) & 0x01;
  358. msg->u.rtpfb.u.dbi.q = (ptr[2] >> 6) & 0x01;
  359. ctx->handler.on_rtcp(ctx->cbparam, msg);
  360. (void)ctx, (void)header, (void)ptr;
  361. return 0;
  362. }
  363. // https://www.rfc-editor.org/rfc/rfc8888.html#section-3.1
  364. /*
  365. 0 1 2 3
  366. 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
  367. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  368. |V=2|P| FMT=11 | PT = 205 | length |
  369. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  370. | SSRC of RTCP packet sender |
  371. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  372. | SSRC of 1st RTP Stream |
  373. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  374. | begin_seq | num_reports |
  375. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  376. |R|ECN| Arrival time offset | ... .
  377. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  378. . .
  379. . .
  380. . .
  381. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  382. | SSRC of nth RTP Stream |
  383. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  384. | begin_seq | num_reports |
  385. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  386. |R|ECN| Arrival time offset | ... |
  387. . .
  388. . .
  389. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  390. | Report Timestamp (32 bits) |
  391. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  392. */
  393. static int rtcp_rtpfb_ccfb_unpack(struct rtp_context* ctx, const rtcp_header_t* header, struct rtcp_msg_t* msg, const uint8_t* ptr, size_t bytes)
  394. {
  395. uint32_t i, num, ssrc;
  396. uint32_t timestamp;
  397. uint16_t begin;
  398. rtcp_ccfb_t* ccfb, ccfb0[32];
  399. timestamp = bytes >= 4 ? nbo_r32(ptr + bytes - 4) : 0; // get last report timestamp
  400. while(bytes >= 8)
  401. {
  402. ssrc = nbo_r32(ptr); // target ssrc
  403. begin = nbo_r16(ptr + 4);
  404. num = nbo_r16(ptr + 6);
  405. ptr += 8;
  406. bytes -= 8;
  407. if ( (num * 2 + 3) / 4 * 4 > bytes) // 4-bytes padding
  408. {
  409. assert(0);
  410. return -1;
  411. }
  412. if (num > sizeof(ccfb0) / sizeof(ccfb0[0]))
  413. {
  414. ccfb = calloc(num, sizeof(*ccfb));
  415. if (!ccfb) return -ENOMEM;
  416. }
  417. else
  418. {
  419. ccfb = ccfb0;
  420. memset(ccfb, 0, sizeof(ccfb[0]) * num);
  421. }
  422. for (i = 0; i < num; i++)
  423. {
  424. ccfb[i].seq = begin + i;
  425. ccfb[i].received = (ptr[i * 2] >> 7) & 0x01;
  426. ccfb[i].ecn = (ptr[i * 2] >> 5) & 0x03;
  427. ccfb[i].ato = ((ptr[i * 2] & 0x1F) << 8) | ptr[i * 2 + 1];
  428. }
  429. msg->u.rtpfb.u.tcc01.timestamp = 0; // fixme
  430. msg->u.rtpfb.u.tcc01.ssrc = ssrc;
  431. msg->u.rtpfb.u.tcc01.begin = begin;
  432. msg->u.rtpfb.u.tcc01.ccfb = ccfb;
  433. msg->u.rtpfb.u.tcc01.count = i;
  434. ctx->handler.on_rtcp(ctx->cbparam, msg);
  435. num = (num + 1) / 2 * 2; // 4-bytes padding
  436. assert(bytes >= num * 2); // check again
  437. ptr += num * 2;
  438. bytes -= num * 2;
  439. if (ccfb && ccfb != ccfb0)
  440. free(ccfb);
  441. }
  442. if (bytes >= 4)
  443. {
  444. timestamp = nbo_r32(ptr);
  445. ptr += 4;
  446. bytes -= 4;
  447. }
  448. (void)ctx, (void)header;
  449. assert(0 == bytes);
  450. return 0;
  451. }
  452. static int rtcp_rtpfb_ccfb_pack(uint32_t ssrc, uint16_t begin, const rtcp_ccfb_t* ccfb, int count, uint32_t timestamp, uint8_t* ptr, uint32_t bytes)
  453. {
  454. int i;
  455. if (bytes < 8 + (uint32_t)count * 2 + 4 || count > 0xFFFF)
  456. return -1;
  457. nbo_w32(ptr, ssrc);
  458. nbo_w16(ptr + 4, begin);
  459. nbo_w16(ptr + 6, (uint16_t)count);
  460. ptr += 8;
  461. for (i = 0; i < count; i++)
  462. {
  463. nbo_w16(ptr, (ccfb[i].received ? 0x8000 : 0) | ((ccfb[i].ecn & 0x03) << 13) | (ccfb->ato & 0x1FFF));
  464. bytes -= 2;
  465. ptr += 2;
  466. }
  467. nbo_w32(ptr, timestamp);
  468. return 8 + count * 2 + 4;
  469. }
  470. // https://datatracker.ietf.org/doc/html/draft-holmer-rmcat-transport-wide-cc-extensions-00#section-3.1
  471. /*
  472. 0 1 2 3
  473. 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
  474. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  475. | fb seq num |r| base sequence number |
  476. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  477. | base receive time | sequence number ack vector |
  478. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  479. | recv delta | recv delta | recv delta |...|
  480. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  481. . .
  482. . .
  483. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  484. | recovery base sequence number | recovery vector |
  485. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  486. */
  487. // https://datatracker.ietf.org/doc/html/draft-holmer-rmcat-transport-wide-cc-extensions#section-3.1
  488. /*
  489. 0 1 2 3
  490. 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
  491. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  492. |V=2|P| FMT=15 | PT=205 | length |
  493. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  494. | SSRC of packet sender |
  495. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  496. | SSRC of media source |
  497. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  498. | base sequence number | packet status count |
  499. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  500. | reference time | fb pkt. count |
  501. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  502. | packet chunk | packet chunk |
  503. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  504. . .
  505. . .
  506. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  507. | packet chunk | recv delta | recv delta |
  508. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  509. . .
  510. . .
  511. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  512. | recv delta | recv delta | zero padding |
  513. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  514. */
  515. static int rtcp_rtpfb_tcc01_unpack(struct rtp_context* ctx, const rtcp_header_t* header, struct rtcp_msg_t* msg, const uint8_t* ptr, size_t bytes)
  516. {
  517. int r;
  518. uint16_t i, j;
  519. uint32_t timestamp;
  520. uint16_t seq, num, chunk;
  521. uint8_t cc;
  522. rtcp_ccfb_t* ccfb, ccfb0[32];
  523. if (bytes < 8)
  524. return -1;
  525. seq = nbo_r16(ptr);
  526. num = nbo_r16(ptr + 2);
  527. timestamp = (ptr[4] << 16) | (ptr[5] << 8) | ptr[6];
  528. cc = ptr[7];
  529. msg->u.rtpfb.u.tcc01.cc = cc;
  530. msg->u.rtpfb.u.tcc01.begin = seq;
  531. msg->u.rtpfb.u.tcc01.timestamp = timestamp | ((timestamp & 0x00800000) ? 0xFF000000 : 0); // signed-24 -> signed-32
  532. if (num > sizeof(ccfb0) / sizeof(ccfb0[0]))
  533. {
  534. ccfb = calloc(num, sizeof(*ccfb));
  535. if (!ccfb) return -ENOMEM;
  536. }
  537. else
  538. {
  539. ccfb = ccfb0;
  540. memset(ccfb, 0, sizeof(ccfb[0]) * num);
  541. }
  542. r = 0;
  543. ptr += 8;
  544. bytes -= 8;
  545. for (i = 0; bytes >= 2 && i < num; bytes -= 2, ptr += 2)
  546. {
  547. chunk = nbo_r16(ptr); // packet chunk
  548. if (0 == (0x8000 & chunk))
  549. {
  550. // Run Length Chunk
  551. for (j = 0; j < (chunk & 0x1FFF) && i < num; j++, i++)
  552. {
  553. ccfb[i].seq = seq++;
  554. ccfb[i].ecn = (chunk >> 13) & 0x03;
  555. ccfb[i].received = ccfb[i].ecn ? 1 : 0;
  556. ccfb[i].ato = 0;
  557. }
  558. }
  559. else
  560. {
  561. // Status Vector Chunk
  562. if (0x4000 & chunk)
  563. {
  564. // two bits
  565. for (j = 0; j < 7 && i < num; j++, i++)
  566. {
  567. ccfb[i].seq = seq++;
  568. ccfb[i].ecn = (chunk >> (2 * (6 - j))) & 0x03;
  569. ccfb[i].received = ccfb[i].ecn ? 1 : 0;
  570. ccfb[i].ato = 0;
  571. }
  572. }
  573. else
  574. {
  575. // one bits
  576. for (j = 0; j < 14 && i < num; j++, i++)
  577. {
  578. ccfb[i].seq = seq++;
  579. ccfb[i].ecn = (chunk & (1 << (13 - j))) ? 0x01 : 0x00; // small delta
  580. ccfb[i].received = ccfb[i].ecn ? 1 : 0;
  581. ccfb[i].ato = 0;
  582. }
  583. }
  584. }
  585. }
  586. for (i = 0; i < num && bytes > 0; i++)
  587. {
  588. if (!ccfb[i].received)
  589. continue;
  590. assert(ccfb[i].ecn == 0x01 || ccfb[i].ecn == 0x02);
  591. if (ccfb[i].ecn == 0x01)
  592. {
  593. ccfb[i].ato = ptr[0] >> 2; // 250us
  594. bytes -= 1;
  595. ptr += 1;
  596. }
  597. else
  598. {
  599. if (bytes < 2)
  600. {
  601. assert(0);
  602. r = -1;
  603. break;
  604. }
  605. ccfb[i].ato = ((int16_t)nbo_r16(ptr)) >> 2; // 250us -> 1/1024(s)
  606. bytes -= 2;
  607. ptr += 2;
  608. }
  609. }
  610. if (0 == r)
  611. {
  612. msg->u.rtpfb.u.tcc01.ccfb = ccfb;
  613. msg->u.rtpfb.u.tcc01.count = num;
  614. ctx->handler.on_rtcp(ctx->cbparam, msg);
  615. }
  616. (void)ctx, (void)header;
  617. if (ccfb && ccfb != ccfb0)
  618. free(ccfb);
  619. return r;
  620. }
  621. static int rtcp_rtpfb_tcc01_pack(uint16_t begin, const rtcp_ccfb_t* ccfb, int count, uint32_t timestamp, uint8_t cc, uint8_t* ptr, uint32_t bytes)
  622. {
  623. int i, n;
  624. int16_t ato;
  625. uint16_t k, chunk, two;
  626. uint32_t seq, received;
  627. const uint8_t* p;
  628. if (bytes < 8 || count < 1)
  629. return -1;
  630. nbo_w16(ptr, begin);
  631. nbo_w16(ptr + 2, (uint16_t)count); // placeholder
  632. nbo_w32(ptr + 4, ((timestamp & 0xFFFFFF) << 8) | cc);
  633. ptr += 8;
  634. p = ptr; // chunk pointer
  635. n = 8;
  636. for (i = 0; i < count && bytes >= 2; i += k)
  637. {
  638. ato = ccfb[i].ato << 2; // 1/1024(s) -> 250us
  639. two = (ccfb[i].received && (uint16_t)ato > 0xFF) ? 2 : 1;
  640. received = ccfb[i].received;
  641. // try Run Length Chunk
  642. for (k = 1; i + k < count && ccfb[i + k].received == ccfb[i].received; k++)
  643. {
  644. ato = ccfb[i + k].ato << 2; // 1/1024(s) -> 250us
  645. two = (ccfb[i + k].received && (uint16_t)ato > 0xFF) ? 2 : two;
  646. received |= ccfb[i + k].received;
  647. }
  648. if (k > 14 / two)
  649. {
  650. // Run Length Chunk
  651. chunk = 0x0000 | (received ? (2 == two ? 0x4000 : 0x2000) : 0x0000) | k;
  652. }
  653. else
  654. {
  655. two = 1; // re-detect
  656. for (k = 0; k < 14 / two && i + k < count; k++)
  657. {
  658. ato = ccfb[i + k].ato << 2; // 1/1024(s) -> 250us
  659. two = (ccfb[i + k].received && (uint16_t)ato > 0xFF) ? 2 : two;
  660. }
  661. ato = (2 == two || k <= 7) ? 1 : 0; // small space/padding
  662. chunk = 0x8000 | (ato ? 0x4000 : 0x0000);
  663. for (k = 0; k < 14 / two && i + k < count; k++)
  664. {
  665. if (ccfb[i + k].received)
  666. {
  667. chunk |= ((uint16_t)(ccfb[i + k].ato << 2) > 0xFF ? 2 : 1) << (ato ? (12 - k * 2) : (13 - k));
  668. }
  669. }
  670. }
  671. nbo_w16(ptr, chunk);
  672. bytes -= 2;
  673. ptr += 2;
  674. n += 2;
  675. }
  676. // parse chunk and write delta
  677. for (i = 0; i < count && bytes >= 1; p += 2)
  678. {
  679. chunk = nbo_r16(p); // packet chunk
  680. if (0 == (0x8000 & chunk))
  681. {
  682. // Run Length Chunk
  683. seq = ccfb[i].seq;
  684. for (k = 0; k < (chunk & 0x1FFF) && i < count; k++)
  685. {
  686. assert(seq + k == ccfb[i].seq);
  687. if (seq + k != ccfb[i].seq)
  688. continue;
  689. ato = ccfb[i].ato << 2; // 1/1024(s) -> 250us
  690. if (chunk & 0x4000)
  691. {
  692. if (bytes < 2)
  693. return -1;
  694. nbo_w16(ptr, ato);
  695. bytes -= 2;
  696. ptr += 2;
  697. n += 2;
  698. }
  699. else if (chunk & 0x2000)
  700. {
  701. if (bytes < 1)
  702. return -1;
  703. ptr[0] = (uint8_t)ato;
  704. bytes -= 1;
  705. ptr += 1;
  706. n += 1;
  707. }
  708. i++;
  709. }
  710. }
  711. else
  712. {
  713. // Status Vector Chunk
  714. if (0x4000 & chunk)
  715. {
  716. // two bits
  717. for (k = 0; k < 7 && i < count && bytes >= 2; k++, i++)
  718. {
  719. if (!ccfb[i].received)
  720. continue;
  721. ato = ccfb[i].ato << 2; // 1/1024(s) -> 250us
  722. two = (chunk >> (2 * (6 - k))) & 0x03;
  723. if (two == 1)
  724. {
  725. ptr[0] = (uint8_t)ato;
  726. bytes -= 1;
  727. ptr += 1;
  728. n += 1;
  729. }
  730. else
  731. {
  732. nbo_w16(ptr, ato);
  733. bytes -= 2;
  734. ptr += 2;
  735. n += 2;
  736. }
  737. }
  738. }
  739. else
  740. {
  741. // one bits
  742. for (k = 0; k < 14 && i < count && bytes >= 1; k++, i++)
  743. {
  744. if (!ccfb[i].received)
  745. continue;
  746. ato = ccfb[i].ato << 2; // 1/1024(s) -> 250us
  747. ptr[0] = (uint8_t)ato;
  748. bytes -= 1;
  749. ptr += 1;
  750. n += 1;
  751. }
  752. }
  753. }
  754. }
  755. // padding
  756. for(k = 0; k < 4 && (n % 4 != 0) && bytes > 0; k++)
  757. {
  758. ptr[0] = ((n + 1) % 4 == 0) ? (uint8_t)k + 1 : 0;
  759. bytes -= 1;
  760. ptr += 1;
  761. n += 1;
  762. }
  763. return n;
  764. }
  765. // https://datatracker.ietf.org/doc/html/rfc4585#section-6.2
  766. /*
  767. * Common Packet Format for Feedback Messages
  768. 0 1 2 3
  769. 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
  770. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  771. |V=2|P| FMT | PT | length |
  772. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  773. | SSRC of packet sender |
  774. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  775. | SSRC of media source |
  776. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  777. : Feedback Control Information (FCI) :
  778. : :
  779. */
  780. void rtcp_rtpfb_unpack(struct rtp_context* ctx, const rtcp_header_t* header, const uint8_t* ptr, size_t bytes)
  781. {
  782. int r;
  783. struct rtcp_msg_t msg;
  784. struct rtp_member* sender;
  785. if (bytes < 8 /*sizeof(rtcp_fci_t)*/)
  786. {
  787. assert(0);
  788. return;
  789. }
  790. msg.type = RTCP_RTPFB | (header->rc << 8);
  791. msg.ssrc = nbo_r32(ptr);
  792. msg.u.rtpfb.media = nbo_r32(ptr + 4);
  793. sender = rtp_sender_fetch(ctx, msg.ssrc);
  794. if (!sender) return; // error
  795. //assert(sender != ctx->self);
  796. switch (header->rc)
  797. {
  798. case RTCP_RTPFB_NACK:
  799. r = rtcp_rtpfb_nack_unpack(ctx, header, &msg, ptr + 8, bytes - 8);
  800. break;
  801. case RTCP_RTPFB_TMMBR:
  802. r = rtcp_rtpfb_tmmbr_unpack(ctx, header, &msg, ptr + 8, bytes - 8);
  803. break;
  804. case RTCP_RTPFB_TMMBN:
  805. r = rtcp_rtpfb_tmmbn_unpack(ctx, header, &msg, ptr + 8, bytes - 8);
  806. break;
  807. case RTCP_RTPFB_SRREQ:
  808. r = rtcp_rtpfb_srreq_unpack(ctx, header, &msg, ptr + 8, bytes - 8);
  809. break;
  810. case RTCP_RTPFB_RAMS:
  811. r = rtcp_rtpfb_rams_unpack(ctx, header, &msg, ptr + 8, bytes - 8);
  812. break;
  813. case RTCP_RTPFB_TLLEI:
  814. r = rtcp_rtpfb_tllei_unpack(ctx, header, &msg, ptr + 8, bytes - 8);
  815. break;
  816. case RTCP_RTPFB_ECN:
  817. r = rtcp_rtpfb_ecn_unpack(ctx, header, &msg, ptr + 8, bytes - 8);
  818. break;
  819. case RTCP_RTPFB_PS:
  820. r = rtcp_rtpfb_ps_unpack(ctx, header, &msg, ptr + 8, bytes - 8);
  821. break;
  822. case RTCP_RTPFB_DBI:
  823. r = rtcp_rtpfb_dbi_unpack(ctx, header, &msg, ptr + 8, bytes - 8);
  824. break;
  825. case RTCP_RTPFB_CCFB:
  826. r = rtcp_rtpfb_ccfb_unpack(ctx, header, &msg, ptr + 4 /*1st ssrc*/, bytes - 4);
  827. break;
  828. case RTCP_RTPFB_TCC01:
  829. r = rtcp_rtpfb_tcc01_unpack(ctx, header, &msg, ptr + 8, bytes - 8);
  830. break;
  831. default:
  832. assert(0);
  833. r = 0;
  834. break;
  835. }
  836. return;
  837. }
  838. int rtcp_rtpfb_pack(struct rtp_context* ctx, uint8_t* data, int bytes, enum rtcp_rtpfb_type_t id, const rtcp_rtpfb_t* rtpfb)
  839. {
  840. int r;
  841. rtcp_header_t header;
  842. (void)ctx;
  843. if (bytes < 4 + 4 + 4)
  844. return 4 + 4 + 4;
  845. switch (id)
  846. {
  847. case RTCP_RTPFB_NACK:
  848. r = rtcp_rtpfb_nack_pack(rtpfb->u.nack.nack, rtpfb->u.nack.count, data + 12, bytes - 12);
  849. break;
  850. case RTCP_RTPFB_TMMBR:
  851. r = rtcp_rtpfb_tmmbr_pack(rtpfb->u.tmmbr.tmmbr, rtpfb->u.tmmbr.count, data + 12, bytes - 12);
  852. break;
  853. case RTCP_RTPFB_TMMBN:
  854. r = rtcp_rtpfb_tmmbn_pack(rtpfb->u.tmmbr.tmmbr, rtpfb->u.tmmbr.count, data + 12, bytes - 12);
  855. break;
  856. case RTCP_RTPFB_TLLEI:
  857. r = rtcp_rtpfb_tllei_pack(rtpfb->u.nack.nack, rtpfb->u.nack.count, data + 12, bytes - 12);
  858. break;
  859. case RTCP_RTPFB_ECN:
  860. r = rtcp_rtpfb_ecn_pack(&rtpfb->u.ecn, data + 12, bytes - 12);
  861. break;
  862. case RTCP_RTPFB_PS:
  863. r = rtcp_rtpfb_ps_pack(rtpfb->u.ps.target, (uint8_t)rtpfb->u.ps.cmd, (uint8_t)rtpfb->u.ps.len, (uint16_t)rtpfb->u.ps.id, (const uint8_t*)rtpfb->u.ps.payload, data + 12, bytes - 12);
  864. break;
  865. case RTCP_RTPFB_CCFB:
  866. r = rtcp_rtpfb_ccfb_pack(rtpfb->u.tcc01.ssrc, (uint16_t)rtpfb->u.tcc01.begin, rtpfb->u.tcc01.ccfb, rtpfb->u.tcc01.count, rtpfb->u.tcc01.timestamp, data + 12, bytes - 12);
  867. break;
  868. case RTCP_RTPFB_TCC01:
  869. r = rtcp_rtpfb_tcc01_pack((uint16_t)rtpfb->u.tcc01.begin, rtpfb->u.tcc01.ccfb, rtpfb->u.tcc01.count, rtpfb->u.tcc01.timestamp, (uint8_t)rtpfb->u.tcc01.cc, data + 12, bytes - 12);
  870. break;
  871. case RTCP_RTPFB_SRREQ:
  872. case RTCP_RTPFB_RAMS:
  873. case RTCP_RTPFB_DBI:
  874. default:
  875. assert(0);
  876. return -1;
  877. }
  878. header.v = 2;
  879. header.p = 0;
  880. header.pt = RTCP_RTPFB;
  881. header.rc = id;
  882. header.length = (r + 8 + 3) / 4;
  883. nbo_write_rtcp_header(data, &header);
  884. nbo_w32(data + 4, ctx->self->ssrc);
  885. //nbo_w32(data + 4, rtpfb->sender);
  886. nbo_w32(data + 8, rtpfb->media);
  887. //assert(8 == (header.length + 1) * 4);
  888. return header.length * 4 + 4;
  889. }
  890. #if defined(_DEBUG) || defined(DEBUG)
  891. static void rtcp_on_rtpfb_test(void* param, const struct rtcp_msg_t* msg)
  892. {
  893. int r;
  894. static uint8_t buffer[1400];
  895. switch (msg->type & 0xFF)
  896. {
  897. case RTCP_RTPFB:
  898. switch ((msg->type >> 8) & 0xFF)
  899. {
  900. case RTCP_RTPFB_NACK:
  901. assert(13 == msg->u.rtpfb.u.nack.count);
  902. assert(msg->u.rtpfb.u.nack.nack[0].pid == 631 && msg->u.rtpfb.u.nack.nack[0].blp == 0x8028);
  903. assert(msg->u.rtpfb.u.nack.nack[1].pid == 648 && msg->u.rtpfb.u.nack.nack[1].blp == 0x0021);
  904. assert(msg->u.rtpfb.u.nack.nack[2].pid == 666 && msg->u.rtpfb.u.nack.nack[2].blp == 0x0008);
  905. assert(msg->u.rtpfb.u.nack.nack[3].pid == 690 && msg->u.rtpfb.u.nack.nack[3].blp == 0x2000);
  906. assert(msg->u.rtpfb.u.nack.nack[4].pid == 734 && msg->u.rtpfb.u.nack.nack[4].blp == 0x0025);
  907. assert(msg->u.rtpfb.u.nack.nack[5].pid == 757 && msg->u.rtpfb.u.nack.nack[5].blp == 0x1100);
  908. assert(msg->u.rtpfb.u.nack.nack[6].pid == 777 && msg->u.rtpfb.u.nack.nack[6].blp == 0x0000);
  909. assert(msg->u.rtpfb.u.nack.nack[7].pid == 826 && msg->u.rtpfb.u.nack.nack[7].blp == 0x0002);
  910. assert(msg->u.rtpfb.u.nack.nack[8].pid == 865 && msg->u.rtpfb.u.nack.nack[8].blp == 0x0000);
  911. assert(msg->u.rtpfb.u.nack.nack[9].pid == 882 && msg->u.rtpfb.u.nack.nack[9].blp == 0x0300);
  912. assert(msg->u.rtpfb.u.nack.nack[10].pid == 907 && msg->u.rtpfb.u.nack.nack[10].blp == 0x0400);
  913. assert(msg->u.rtpfb.u.nack.nack[11].pid == 931 && msg->u.rtpfb.u.nack.nack[11].blp == 0x0000);
  914. assert(msg->u.rtpfb.u.nack.nack[12].pid == 963 && msg->u.rtpfb.u.nack.nack[12].blp == 0x006d);
  915. r = rtcp_rtpfb_nack_pack(msg->u.rtpfb.u.nack.nack, msg->u.rtpfb.u.nack.count, buffer, sizeof(buffer));
  916. assert(r > 0 && 0 == memcmp(buffer, param, r));
  917. break;
  918. case RTCP_RTPFB_TMMBR:
  919. assert(1 == msg->u.rtpfb.u.tmmbr.count);
  920. assert(0x23456789 == msg->u.rtpfb.u.tmmbr.tmmbr[0].ssrc && 2 == msg->u.rtpfb.u.tmmbr.tmmbr[0].exp && 78000 == msg->u.rtpfb.u.tmmbr.tmmbr[0].mantissa && 312000 == (msg->u.rtpfb.u.tmmbr.tmmbr[0].mantissa << msg->u.rtpfb.u.tmmbr.tmmbr[0].exp) && 0x1fe == msg->u.rtpfb.u.tmmbr.tmmbr[0].overhead);
  921. r = rtcp_rtpfb_tmmbr_pack(msg->u.rtpfb.u.tmmbr.tmmbr, msg->u.rtpfb.u.tmmbr.count, buffer, sizeof(buffer));
  922. assert(8 == r && 0 == memcmp(buffer, param, r));
  923. break;
  924. case RTCP_RTPFB_TMMBN:
  925. assert(1 == msg->u.rtpfb.u.tmmbr.count);
  926. assert(0x23456789 == msg->u.rtpfb.u.tmmbr.tmmbr[0].ssrc && 2 == msg->u.rtpfb.u.tmmbr.tmmbr[0].exp && 78000 == msg->u.rtpfb.u.tmmbr.tmmbr[0].mantissa && 312000 == (msg->u.rtpfb.u.tmmbr.tmmbr[0].mantissa << msg->u.rtpfb.u.tmmbr.tmmbr[0].exp) && 0x1fe == msg->u.rtpfb.u.tmmbr.tmmbr[0].overhead);
  927. r = rtcp_rtpfb_tmmbr_pack(msg->u.rtpfb.u.tmmbr.tmmbr, msg->u.rtpfb.u.tmmbr.count, buffer, sizeof(buffer));
  928. assert(8 == r && 0 == memcmp(buffer, param, r));
  929. break;
  930. case RTCP_RTPFB_TCC01:
  931. assert(1767 == msg->u.rtpfb.u.tcc01.begin && 4114000 == msg->u.rtpfb.u.tcc01.timestamp && 101 == msg->u.rtpfb.u.tcc01.count && 42 == msg->u.rtpfb.u.tcc01.cc);
  932. assert(msg->u.rtpfb.u.tcc01.ccfb[0].seq == 1767 && msg->u.rtpfb.u.tcc01.ccfb[0].received == 0);
  933. assert(msg->u.rtpfb.u.tcc01.ccfb[1].seq == 1768 && msg->u.rtpfb.u.tcc01.ccfb[1].received == 1 && msg->u.rtpfb.u.tcc01.ccfb[1].ato == 21);
  934. assert(msg->u.rtpfb.u.tcc01.ccfb[2].seq == 1769 && msg->u.rtpfb.u.tcc01.ccfb[2].received == 1 && msg->u.rtpfb.u.tcc01.ccfb[2].ato == 0);
  935. assert(msg->u.rtpfb.u.tcc01.ccfb[3].seq == 1770 && msg->u.rtpfb.u.tcc01.ccfb[3].received == 0);
  936. assert(msg->u.rtpfb.u.tcc01.ccfb[4].seq == 1771 && msg->u.rtpfb.u.tcc01.ccfb[4].received == 1 && msg->u.rtpfb.u.tcc01.ccfb[4].ato == 0);
  937. assert(msg->u.rtpfb.u.tcc01.ccfb[5].seq == 1772 && msg->u.rtpfb.u.tcc01.ccfb[5].received == 0);
  938. assert(msg->u.rtpfb.u.tcc01.ccfb[6].seq == 1773 && msg->u.rtpfb.u.tcc01.ccfb[6].received == 1 && msg->u.rtpfb.u.tcc01.ccfb[6].ato == 26);
  939. r = rtcp_rtpfb_tcc01_pack((uint16_t)msg->u.rtpfb.u.tcc01.begin, msg->u.rtpfb.u.tcc01.ccfb, msg->u.rtpfb.u.tcc01.count, msg->u.rtpfb.u.tcc01.timestamp, (uint8_t)msg->u.rtpfb.u.tcc01.cc, buffer, sizeof(buffer));
  940. assert(104 == r && 0 == memcmp(buffer, param, r));
  941. break;
  942. case 30: // test only
  943. assert(5 == msg->u.rtpfb.u.tcc01.begin && 5 == msg->u.rtpfb.u.tcc01.timestamp && 7 == msg->u.rtpfb.u.tcc01.count && 0 == msg->u.rtpfb.u.tcc01.cc);
  944. assert(msg->u.rtpfb.u.tcc01.ccfb[0].seq == 5 && msg->u.rtpfb.u.tcc01.ccfb[0].received == 1 && msg->u.rtpfb.u.tcc01.ccfb[1].ato == 0);
  945. assert(msg->u.rtpfb.u.tcc01.ccfb[1].seq == 6 && msg->u.rtpfb.u.tcc01.ccfb[1].received == 0);
  946. assert(msg->u.rtpfb.u.tcc01.ccfb[2].seq == 7 && msg->u.rtpfb.u.tcc01.ccfb[2].received == 1 && msg->u.rtpfb.u.tcc01.ccfb[2].ato == 128);
  947. assert(msg->u.rtpfb.u.tcc01.ccfb[3].seq == 8 && msg->u.rtpfb.u.tcc01.ccfb[3].received == 1 && msg->u.rtpfb.u.tcc01.ccfb[3].ato == 64);
  948. assert(msg->u.rtpfb.u.tcc01.ccfb[4].seq == 9 && msg->u.rtpfb.u.tcc01.ccfb[4].received == 0);
  949. assert(msg->u.rtpfb.u.tcc01.ccfb[5].seq == 10 && msg->u.rtpfb.u.tcc01.ccfb[5].received == 0);
  950. assert(msg->u.rtpfb.u.tcc01.ccfb[6].seq == 11 && msg->u.rtpfb.u.tcc01.ccfb[6].received == 1 && msg->u.rtpfb.u.tcc01.ccfb[6].ato == 256);
  951. r = rtcp_rtpfb_tcc01_pack((uint16_t)msg->u.rtpfb.u.tcc01.begin, msg->u.rtpfb.u.tcc01.ccfb, msg->u.rtpfb.u.tcc01.count, msg->u.rtpfb.u.tcc01.timestamp, (uint8_t)msg->u.rtpfb.u.tcc01.cc, buffer, sizeof(buffer));
  952. assert(20 == r && 0 == memcmp(buffer, param, r));
  953. break;
  954. case 31: // test only
  955. assert(248 == msg->u.rtpfb.u.tcc01.begin && -5546573 == msg->u.rtpfb.u.tcc01.timestamp && 128 == msg->u.rtpfb.u.tcc01.count && 1 == msg->u.rtpfb.u.tcc01.cc);
  956. assert(msg->u.rtpfb.u.tcc01.ccfb[0].seq == 248 && msg->u.rtpfb.u.tcc01.ccfb[0].received == 1 && msg->u.rtpfb.u.tcc01.ccfb[0].ato == 33);
  957. assert(msg->u.rtpfb.u.tcc01.ccfb[1].seq == 249 && msg->u.rtpfb.u.tcc01.ccfb[1].received == 0);
  958. assert(msg->u.rtpfb.u.tcc01.ccfb[125].seq == 373 && msg->u.rtpfb.u.tcc01.ccfb[125].received == 1 && msg->u.rtpfb.u.tcc01.ccfb[125].ato == -50);
  959. assert(msg->u.rtpfb.u.tcc01.ccfb[126].seq == 374 && msg->u.rtpfb.u.tcc01.ccfb[126].received == 0);
  960. assert(msg->u.rtpfb.u.tcc01.ccfb[127].seq == 375 && msg->u.rtpfb.u.tcc01.ccfb[127].received == 1 && msg->u.rtpfb.u.tcc01.ccfb[127].ato == 65);
  961. r = rtcp_rtpfb_tcc01_pack((uint16_t)msg->u.rtpfb.u.tcc01.begin, msg->u.rtpfb.u.tcc01.ccfb, msg->u.rtpfb.u.tcc01.count, msg->u.rtpfb.u.tcc01.timestamp, (uint8_t)msg->u.rtpfb.u.tcc01.cc, buffer, sizeof(buffer));
  962. assert(20 == r && 0 == memcmp(buffer, param, r));
  963. break;
  964. case RTCP_RTPFB_CCFB:
  965. assert(0);
  966. break;
  967. default:
  968. break;
  969. }
  970. break;
  971. default:
  972. assert(0);
  973. }
  974. }
  975. static void rtcp_rtpfb_nack_test(void)
  976. {
  977. //rtcp_nack_t* nack;
  978. //const uint8_t data[] = { 0x81, 0xcd, 0x00, 0x08, 0x84, 0x68, 0xc2, 0x4c, 0x84, 0x68, 0xc2, 0x4c, 0x03, 0x27, 0x7f, 0xff, 0x03, 0x38, 0xff, 0xf7, 0x03, 0x49, 0xff, 0xff, 0x03, 0x5a, 0xff, 0xff, 0x03, 0x6b, 0x7f, 0xbf, 0x03, 0x7c, 0x00, 0x0f };
  979. //assert(0 == rtcp_rtpfb_nack_unpack(NULL, NULL, 0, 0, data, sizeof(data)));
  980. //assert(nack[0].pid == 807 && nack[0].blp == 0x7fff);
  981. //assert(nack[1].pid == 824 && nack[1].blp == 0xfff7);
  982. //assert(nack[2].pid == 841 && nack[2].blp == 0xffff);
  983. //assert(nack[3].pid == 858 && nack[3].blp == 0xffff);
  984. //assert(nack[4].pid == 875 && nack[4].blp == 0x7fbf);
  985. //assert(nack[5].pid == 892 && nack[5].blp == 0x000f);
  986. const uint8_t data[] = { 0x02, 0x77, 0x80, 0x28, 0x02, 0x88, 0x00, 0x21, 0x02, 0x9a, 0x00, 0x08, 0x02, 0xb2, 0x20, 0x00, 0x02, 0xde, 0x00, 0x25, 0x02, 0xf5, 0x11, 0x00, 0x03, 0x09, 0x00, 0x00, 0x03, 0x3a, 0x00, 0x02, 0x03, 0x61, 0x00, 0x00, 0x03, 0x72, 0x03, 0x00, 0x03, 0x8b, 0x04, 0x00, 0x03, 0xa3, 0x00, 0x00, 0x03, 0xc3, 0x00, 0x6d };
  987. struct rtcp_msg_t msg;
  988. struct rtp_context rtp;
  989. rtp.handler.on_rtcp = rtcp_on_rtpfb_test;
  990. rtp.cbparam = (void*)data;
  991. msg.type = (RTCP_RTPFB_NACK << 8) | RTCP_RTPFB;
  992. assert(0 == rtcp_rtpfb_nack_unpack(&rtp, NULL, &msg, data, sizeof(data)));
  993. }
  994. static void rtcp_rtpfb_tmmbr_test(void)
  995. {
  996. const uint8_t data[] = { 0x23, 0x45, 0x67, 0x89, 0x0a, 0x61, 0x61, 0xfe };
  997. struct rtcp_msg_t msg;
  998. struct rtp_context rtp;
  999. rtp.handler.on_rtcp = rtcp_on_rtpfb_test;
  1000. rtp.cbparam = (void*)data;
  1001. msg.type = (RTCP_RTPFB_TMMBR << 8) | RTCP_RTPFB;
  1002. assert(0 == rtcp_rtpfb_tmmbr_unpack(&rtp, NULL, &msg, data, sizeof(data)));
  1003. }
  1004. static void rtcp_rtpfb_tmmbn_test(void)
  1005. {
  1006. const uint8_t data[] = { 0x23, 0x45, 0x67, 0x89, 0x0a, 0x61, 0x61, 0xfe };
  1007. struct rtcp_msg_t msg;
  1008. struct rtp_context rtp;
  1009. rtp.handler.on_rtcp = rtcp_on_rtpfb_test;
  1010. rtp.cbparam = (void*)data;
  1011. msg.type = (RTCP_RTPFB_TMMBN << 8) | RTCP_RTPFB;
  1012. assert(0 == rtcp_rtpfb_tmmbn_unpack(&rtp, NULL, &msg, data, sizeof(data)));
  1013. }
  1014. static void rtcp_rtpfb_tcc01_test(void)
  1015. {
  1016. //rtcp_ccfb_t* ccfb;
  1017. //const uint8_t data[] = { 0x8f, 0xcd, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x84, 0x68, 0xc2, 0x49, 0x23, 0x8b, 0x01, 0x8b, 0x3e, 0x08, 0x49, 0x09, 0xd9, 0x00, 0x00, 0x0e, 0xa0, 0x00, 0x00, 0x1c, 0xa0, 0x00, 0x00, 0xe6, 0xa0, 0x00, 0x00, 0x49, 0x20, 0x01, 0x2c, 0xff, 0xf0, 0x0c, 0x44, 0x94, 0x8c, 0x50, 0x00, 0x00 };
  1018. //assert(0 == rtcp_rtpfb_tcc01_unpack(NULL, NULL, 0, 0, data, sizeof(data)));
  1019. //assert(395 == num && ccfb[0].seq == 9099 && ccfb[0].received && ccfb[1].ecn == 0x01 && ccfb[0].ato == 11);
  1020. //assert(cfb[1].seq == 9100 && ccfb[1].received && ccfb[1].ecn == 0x02 && ccfb[1].ato == -4);
  1021. const uint8_t data[] = { 0x06, 0xe7, 0x00, 0x65, 0x3e, 0xc6, 0x50, 0x2a, 0x9a, 0xff, 0x20, 0x16, 0x97, 0x68, 0xbc, 0xab, 0xa7, 0xfe, 0x20, 0x12, 0xc1, 0x50, 0x54, 0x00, 0x00, 0x68, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x01 };
  1022. struct rtcp_msg_t msg;
  1023. struct rtp_context rtp;
  1024. rtp.handler.on_rtcp = rtcp_on_rtpfb_test;
  1025. rtp.cbparam = (void*)data;
  1026. msg.type = (RTCP_RTPFB_TCC01 << 8) | RTCP_RTPFB;
  1027. assert(0 == rtcp_rtpfb_tcc01_unpack(&rtp, NULL, &msg, data, sizeof(data)));
  1028. // 11-768ms, 8-512ms, 7-448ms, 5-320ms
  1029. const uint8_t data2[] = { 0x00, 0x05, 0x00, 0x07, 0x00, 0x00, 0x05, 0x00, 0xd2, 0x82, 0x00, 0x02, 0x00, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x03 };
  1030. rtp.cbparam = (void*)data2;
  1031. msg.type = (30 << 8) | RTCP_RTPFB;
  1032. assert(0 == rtcp_rtpfb_tcc01_unpack(&rtp, NULL, &msg, data2, sizeof(data2)));
  1033. // 248-33ms, 373-(-50)ms, 375-65ms, timestamp: -5546573
  1034. const uint8_t data3[] = { 0x00, 0xf8, 0x00, 0x80, 0xab, 0x5d, 0xb3, 0x01, 0xa0, 0x00, 0x00, 0x6f, 0xe2, 0x00, 0x84, 0xff, 0x38, 0x01, 0x04, 0x01 };
  1035. rtp.cbparam = (void*)data3;
  1036. msg.type = (31 << 8) | RTCP_RTPFB;
  1037. assert(0 == rtcp_rtpfb_tcc01_unpack(&rtp, NULL, &msg, data3, sizeof(data3)));
  1038. }
  1039. void rtcp_rtpfb_test(void)
  1040. {
  1041. rtcp_rtpfb_nack_test();
  1042. rtcp_rtpfb_tmmbr_test();
  1043. rtcp_rtpfb_tmmbn_test();
  1044. rtcp_rtpfb_tcc01_test();
  1045. }
  1046. #endif