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.

137 lines
3.9KB

  1. /// RFC3016 RTP Payload Format for MPEG-4 Audio/Visual Streams
  2. /// RFC6416 RTP Payload Format for MPEG-4 Audio/Visual Streams
  3. ///
  4. /// MPEG-4 Audio streams MUST be formatted LATM (Lowoverhead
  5. /// MPEG-4 Audio Transport Multiplex)[14496 - 3] streams, and the
  6. /// LATM-based streams are then mapped onto RTP packets
  7. #include "rtp-packet.h"
  8. #include "rtp-profile.h"
  9. #include "rtp-payload-internal.h"
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <assert.h>
  13. #include <errno.h>
  14. struct rtp_encode_mp4a_latm_t
  15. {
  16. struct rtp_packet_t pkt;
  17. struct rtp_payload_t handler;
  18. void* cbparam;
  19. int size;
  20. };
  21. static void* rtp_mp4a_latm_pack_create(int size, uint8_t pt, uint16_t seq, uint32_t ssrc, struct rtp_payload_t *handler, void* cbparam)
  22. {
  23. struct rtp_encode_mp4a_latm_t *packer;
  24. packer = (struct rtp_encode_mp4a_latm_t *)calloc(1, sizeof(*packer));
  25. if (!packer) return NULL;
  26. memcpy(&packer->handler, handler, sizeof(packer->handler));
  27. packer->cbparam = cbparam;
  28. packer->size = size;
  29. packer->pkt.rtp.v = RTP_VERSION;
  30. packer->pkt.rtp.pt = pt;
  31. packer->pkt.rtp.seq = seq;
  32. packer->pkt.rtp.ssrc = ssrc;
  33. return packer;
  34. }
  35. static void rtp_mp4a_latm_pack_destroy(void* pack)
  36. {
  37. struct rtp_encode_mp4a_latm_t *packer;
  38. packer = (struct rtp_encode_mp4a_latm_t *)pack;
  39. #if defined(_DEBUG) || defined(DEBUG)
  40. memset(packer, 0xCC, sizeof(*packer));
  41. #endif
  42. free(packer);
  43. }
  44. static void rtp_mp4a_latm_pack_get_info(void* pack, uint16_t* seq, uint32_t* timestamp)
  45. {
  46. struct rtp_encode_mp4a_latm_t *packer;
  47. packer = (struct rtp_encode_mp4a_latm_t *)pack;
  48. *seq = (uint16_t)packer->pkt.rtp.seq;
  49. *timestamp = packer->pkt.rtp.timestamp;
  50. }
  51. static int rtp_mp4a_latm_pack_input(void* pack, const void* data, int bytes, uint32_t timestamp)
  52. {
  53. int r;
  54. int n, len;
  55. uint8_t *rtp;
  56. uint8_t hd[400]; // 100KB
  57. const uint8_t *ptr;
  58. struct rtp_encode_mp4a_latm_t *packer;
  59. packer = (struct rtp_encode_mp4a_latm_t *)pack;
  60. assert(packer->pkt.rtp.timestamp != timestamp || !packer->pkt.payload /*first packet*/);
  61. packer->pkt.rtp.timestamp = timestamp; //(uint32_t)(time * KHz); // ms -> 90KHZ (RFC2250 section2 p2)
  62. r = 0;
  63. ptr = (const uint8_t *)data;
  64. #if defined(RTP_MP4A_LATM_SKIP_ADTS)
  65. if (0xFF == ptr[0] && 0xF0 == (ptr[1] & 0xF0) && bytes > 7)
  66. {
  67. // skip ADTS header
  68. assert(bytes == (((ptr[3] & 0x03) << 11) | (ptr[4] << 3) | ((ptr[5] >> 5) & 0x07)));
  69. ptr += 7;
  70. bytes -= 7;
  71. }
  72. #endif
  73. // ISO/IEC 14496-3:200X(E)
  74. // Table 1.44 - Syntax of PayloadLengthInfo() (p84)
  75. len = bytes / 255 + 1;
  76. if (len > sizeof(hd))
  77. {
  78. assert(0);
  79. return -E2BIG; // invalid packet
  80. }
  81. memset(hd, 255, len - 1);
  82. hd[len - 1] = bytes % 255;
  83. for (; 0 == r && bytes > 0; ++packer->pkt.rtp.seq)
  84. {
  85. packer->pkt.payload = ptr;
  86. packer->pkt.payloadlen = (bytes + len + RTP_FIXED_HEADER) <= packer->size ? bytes : (packer->size - len - RTP_FIXED_HEADER);
  87. ptr += packer->pkt.payloadlen;
  88. bytes -= packer->pkt.payloadlen;
  89. n = RTP_FIXED_HEADER + len + packer->pkt.payloadlen;
  90. rtp = (uint8_t*)packer->handler.alloc(packer->cbparam, n);
  91. if (!rtp) return -ENOMEM;
  92. // Marker (M) bit: The marker bit indicates audioMuxElement boundaries.
  93. // It is set to 1 to indicate that the RTP packet contains a complete
  94. // audioMuxElement or the last fragment of an audioMuxElement.
  95. packer->pkt.rtp.m = (0 == bytes) ? 1 : 0;
  96. n = rtp_packet_serialize_header(&packer->pkt, rtp, n);
  97. if (n != RTP_FIXED_HEADER)
  98. {
  99. assert(0);
  100. return -1;
  101. }
  102. if (len > 0) memcpy(rtp + n, hd, len);
  103. memcpy(rtp + n + len, packer->pkt.payload, packer->pkt.payloadlen);
  104. r = packer->handler.packet(packer->cbparam, rtp, n + len + packer->pkt.payloadlen, packer->pkt.rtp.timestamp, 0);
  105. packer->handler.free(packer->cbparam, rtp);
  106. len = 0;
  107. }
  108. return r;
  109. }
  110. struct rtp_payload_encode_t *rtp_mp4a_latm_encode()
  111. {
  112. static struct rtp_payload_encode_t encode = {
  113. rtp_mp4a_latm_pack_create,
  114. rtp_mp4a_latm_pack_destroy,
  115. rtp_mp4a_latm_pack_get_info,
  116. rtp_mp4a_latm_pack_input,
  117. };
  118. return &encode;
  119. }