You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
6.0KB

  1. // RFC6184 RTP Payload Format for H.264 Video
  2. //
  3. // 6.2. Single NAL Unit Mode (All receivers MUST support this mode)
  4. // packetization-mode media type parameter is equal to 0 or the packetization - mode is not present.
  5. // Only single NAL unit packets MAY be used in this mode.
  6. // STAPs, MTAPs, and FUs MUST NOT be used.
  7. // The transmission order of single NAL unit packets MUST comply with the NAL unit decoding order.
  8. // 6.3. Non-Interleaved Mode (This mode SHOULD be supported)
  9. // packetization-mode media type parameter is equal to 1.
  10. // Only single NAL unit packets, STAP - As, and FU - As MAY be used in this mode.
  11. // STAP-Bs, MTAPs, and FU-Bs MUST NOT be used.
  12. // The transmission order of NAL units MUST comply with the NAL unit decoding order
  13. // 6.4. Interleaved Mode
  14. // packetization-mode media type parameter is equal to 2.
  15. // STAP-Bs, MTAPs, FU-As, and FU-Bs MAY be used.
  16. // STAP-As and single NAL unit packets MUST NOT be used.
  17. // The transmission order of packets and NAL units is constrained as specified in Section 5.5.
  18. //
  19. // 5.1. RTP Header Usage (p10)
  20. // The RTP timestamp is set to the sampling timestamp of the content. A 90 kHz clock rate MUST be used.
  21. #include "rtp-packet.h"
  22. #include "rtp-payload-internal.h"
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <assert.h>
  26. #include <errno.h>
  27. #define KHz 90 // 90000Hz
  28. #define FU_START 0x80
  29. #define FU_END 0x40
  30. #define N_FU_HEADER 2
  31. int rtp_h264_annexb_nalu(const void* h264, int bytes, int (*handler)(void* param, const uint8_t* nalu, int bytes, int last), void* param);
  32. struct rtp_encode_h264_t
  33. {
  34. struct rtp_packet_t pkt;
  35. struct rtp_payload_t handler;
  36. void* cbparam;
  37. int size;
  38. };
  39. static void* rtp_h264_pack_create(int size, uint8_t pt, uint16_t seq, uint32_t ssrc, struct rtp_payload_t *handler, void* cbparam)
  40. {
  41. struct rtp_encode_h264_t *packer;
  42. packer = (struct rtp_encode_h264_t *)calloc(1, sizeof(*packer));
  43. if(!packer) return NULL;
  44. memcpy(&packer->handler, handler, sizeof(packer->handler));
  45. packer->cbparam = cbparam;
  46. packer->size = size;
  47. packer->pkt.rtp.v = RTP_VERSION;
  48. packer->pkt.rtp.pt = pt;
  49. packer->pkt.rtp.seq = seq;
  50. packer->pkt.rtp.ssrc = ssrc;
  51. return packer;
  52. }
  53. static void rtp_h264_pack_destroy(void* pack)
  54. {
  55. struct rtp_encode_h264_t *packer;
  56. packer = (struct rtp_encode_h264_t *)pack;
  57. #if defined(_DEBUG) || defined(DEBUG)
  58. memset(packer, 0xCC, sizeof(*packer));
  59. #endif
  60. free(packer);
  61. }
  62. static void rtp_h264_pack_get_info(void* pack, uint16_t* seq, uint32_t* timestamp)
  63. {
  64. struct rtp_encode_h264_t *packer;
  65. packer = (struct rtp_encode_h264_t *)pack;
  66. *seq = (uint16_t)packer->pkt.rtp.seq;
  67. *timestamp = packer->pkt.rtp.timestamp;
  68. }
  69. static int rtp_h264_pack_nalu(struct rtp_encode_h264_t *packer, const uint8_t* nalu, int bytes, int mark)
  70. {
  71. int r, n;
  72. uint8_t *rtp;
  73. packer->pkt.payload = nalu;
  74. packer->pkt.payloadlen = bytes;
  75. n = RTP_FIXED_HEADER + packer->pkt.payloadlen;
  76. rtp = (uint8_t*)packer->handler.alloc(packer->cbparam, n);
  77. if (!rtp) return -ENOMEM;
  78. //packer->pkt.rtp.m = 1; // set marker flag
  79. packer->pkt.rtp.m = (*nalu & 0x1f) <= 5 ? mark : 0; // VCL only
  80. n = rtp_packet_serialize(&packer->pkt, rtp, n);
  81. if (n != RTP_FIXED_HEADER + packer->pkt.payloadlen)
  82. {
  83. assert(0);
  84. return -1;
  85. }
  86. ++packer->pkt.rtp.seq;
  87. r = packer->handler.packet(packer->cbparam, rtp, n, packer->pkt.rtp.timestamp, 0);
  88. packer->handler.free(packer->cbparam, rtp);
  89. return r;
  90. }
  91. static int rtp_h264_pack_fu_a(struct rtp_encode_h264_t *packer, const uint8_t* nalu, int bytes, int mark)
  92. {
  93. int r, n;
  94. unsigned char *rtp;
  95. // RFC6184 5.3. NAL Unit Header Usage: Table 2 (p15)
  96. // RFC6184 5.8. Fragmentation Units (FUs) (p29)
  97. uint8_t fu_indicator = (*nalu & 0xE0) | 28; // FU-A
  98. uint8_t fu_header = *nalu & 0x1F;
  99. r = 0;
  100. nalu += 1; // skip NAL Unit Type byte
  101. bytes -= 1;
  102. assert(bytes > 0);
  103. // FU-A start
  104. for (fu_header |= FU_START; 0 == r && bytes > 0; ++packer->pkt.rtp.seq)
  105. {
  106. if (bytes + RTP_FIXED_HEADER <= packer->size - N_FU_HEADER)
  107. {
  108. assert(0 == (fu_header & FU_START));
  109. fu_header = FU_END | (fu_header & 0x1F); // FU-A end
  110. packer->pkt.payloadlen = bytes;
  111. }
  112. else
  113. {
  114. packer->pkt.payloadlen = packer->size - RTP_FIXED_HEADER - N_FU_HEADER;
  115. }
  116. packer->pkt.payload = nalu;
  117. n = RTP_FIXED_HEADER + N_FU_HEADER + packer->pkt.payloadlen;
  118. rtp = (uint8_t*)packer->handler.alloc(packer->cbparam, n);
  119. if (!rtp) return -ENOMEM;
  120. packer->pkt.rtp.m = (FU_END & fu_header) ? mark : 0; // set marker flag
  121. n = rtp_packet_serialize_header(&packer->pkt, rtp, n);
  122. if (n != RTP_FIXED_HEADER)
  123. {
  124. assert(0);
  125. return -1;
  126. }
  127. /*fu_indicator + fu_header*/
  128. rtp[n + 0] = fu_indicator;
  129. rtp[n + 1] = fu_header;
  130. memcpy(rtp + n + N_FU_HEADER, packer->pkt.payload, packer->pkt.payloadlen);
  131. r = packer->handler.packet(packer->cbparam, rtp, n + N_FU_HEADER + packer->pkt.payloadlen, packer->pkt.rtp.timestamp, 0);
  132. packer->handler.free(packer->cbparam, rtp);
  133. bytes -= packer->pkt.payloadlen;
  134. nalu += packer->pkt.payloadlen;
  135. fu_header &= 0x1F; // clear flags
  136. }
  137. return r;
  138. }
  139. static int rtp_h264_pack_handler(void* pack, const uint8_t* nalu, int bytes, int last)
  140. {
  141. struct rtp_encode_h264_t* packer;
  142. packer = (struct rtp_encode_h264_t*)pack;
  143. if (bytes + RTP_FIXED_HEADER <= packer->size)
  144. {
  145. // single NAl unit packet
  146. return rtp_h264_pack_nalu(packer, nalu, bytes, last ? 1 : 0);
  147. }
  148. else
  149. {
  150. return rtp_h264_pack_fu_a(packer, nalu, bytes, last ? 1 : 0);
  151. }
  152. }
  153. static int rtp_h264_pack_input(void* pack, const void* h264, int bytes, uint32_t timestamp)
  154. {
  155. struct rtp_encode_h264_t *packer;
  156. packer = (struct rtp_encode_h264_t *)pack;
  157. // assert(packer->pkt.rtp.timestamp != timestamp || !packer->pkt.payload /*first packet*/);
  158. packer->pkt.rtp.timestamp = timestamp; //(uint32_t)time * KHz; // ms -> 90KHZ
  159. return rtp_h264_annexb_nalu(h264, bytes, rtp_h264_pack_handler, packer);
  160. }
  161. struct rtp_payload_encode_t *rtp_h264_encode()
  162. {
  163. static struct rtp_payload_encode_t packer = {
  164. rtp_h264_pack_create,
  165. rtp_h264_pack_destroy,
  166. rtp_h264_pack_get_info,
  167. rtp_h264_pack_input,
  168. };
  169. return &packer;
  170. }