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.

rtp-ts-pack.c 3.4KB

5 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /// RFC3555 MIME Type Registration of RTP Payload Formats
  2. /// 4.2.11 Registration of MIME media type video/MP2P (p40)
  3. ///
  4. /// RFC2250 2. Encapsulation of MPEG System and Transport Streams (p3)
  5. /// 1. Each RTP packet will contain a timestamp derived from the sender's 90KHz clock reference
  6. /// 2. For MPEG2 Program streams and MPEG1 system streams there are no packetization restrictions;
  7. /// these streams are treated as a packetized stream of bytes.
  8. ///
  9. /// 2.1 RTP header usage (p4)
  10. /// 32 bit 90K Hz timestamp representing the target transmission time for the first byte of the packet
  11. #include "rtp-packet.h"
  12. #include "rtp-profile.h"
  13. #include "rtp-payload-internal.h"
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <assert.h>
  17. #include <errno.h>
  18. #define TS_PACKET_SIZE 188
  19. #define KHz 90 // 90000Hz
  20. struct rtp_encode_ts_t
  21. {
  22. struct rtp_packet_t pkt;
  23. struct rtp_payload_t handler;
  24. void* cbparam;
  25. int size;
  26. };
  27. static void* rtp_ts_pack_create(int size, uint8_t pt, uint16_t seq, uint32_t ssrc, struct rtp_payload_t *handler, void* cbparam)
  28. {
  29. struct rtp_encode_ts_t *packer;
  30. packer = (struct rtp_encode_ts_t *)calloc(1, sizeof(*packer));
  31. if (!packer) return NULL;
  32. //assert(pt == RTP_PAYLOAD_MP2T);
  33. if (RTP_PAYLOAD_MP2T == pt)
  34. {
  35. size -= RTP_FIXED_HEADER;
  36. size = size / TS_PACKET_SIZE * TS_PACKET_SIZE;
  37. size += RTP_FIXED_HEADER;
  38. if (size < 64)
  39. {
  40. free(packer);
  41. return NULL;
  42. }
  43. }
  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_ts_pack_destroy(void* pack)
  54. {
  55. struct rtp_encode_ts_t *packer;
  56. packer = (struct rtp_encode_ts_t *)pack;
  57. #if defined(_DEBUG) || defined(DEBUG)
  58. memset(packer, 0xCC, sizeof(*packer));
  59. #endif
  60. free(packer);
  61. }
  62. static void rtp_ts_pack_get_info(void* pack, uint16_t* seq, uint32_t* timestamp)
  63. {
  64. struct rtp_encode_ts_t *packer;
  65. packer = (struct rtp_encode_ts_t *)pack;
  66. *seq = (uint16_t)packer->pkt.rtp.seq;
  67. *timestamp = packer->pkt.rtp.timestamp;
  68. }
  69. static int rtp_ts_pack_input(void* pack, const void* data, int bytes, uint32_t timestamp)
  70. {
  71. int r, n;
  72. uint8_t *rtp;
  73. const uint8_t *ptr;
  74. struct rtp_encode_ts_t *packer;
  75. packer = (struct rtp_encode_ts_t *)pack;
  76. packer->pkt.rtp.timestamp = timestamp; //(uint32_t)(time * KHz); // ms -> 90KHZ (RFC2250 section2 p2)
  77. r = 0;
  78. for (ptr = (const uint8_t *)data; 0 == r && bytes > 0; ++packer->pkt.rtp.seq)
  79. {
  80. packer->pkt.payload = ptr;
  81. packer->pkt.payloadlen = (bytes + RTP_FIXED_HEADER) <= packer->size ? bytes : (packer->size - RTP_FIXED_HEADER);
  82. ptr += packer->pkt.payloadlen;
  83. bytes -= packer->pkt.payloadlen;
  84. n = RTP_FIXED_HEADER + packer->pkt.payloadlen;
  85. rtp = (uint8_t*)packer->handler.alloc(packer->cbparam, n);
  86. if (!rtp) return -ENOMEM;
  87. // M bit: Set to 1 whenever the timestamp is discontinuous
  88. //packer->pkt.rtp.m = (bytes <= packer->size) ? 1 : 0;
  89. packer->pkt.rtp.m = 0;
  90. n = rtp_packet_serialize(&packer->pkt, rtp, n);
  91. if (n != RTP_FIXED_HEADER + packer->pkt.payloadlen)
  92. {
  93. assert(0);
  94. return -1;
  95. }
  96. r = packer->handler.packet(packer->cbparam, rtp, n, packer->pkt.rtp.timestamp, 0);
  97. packer->handler.free(packer->cbparam, rtp);
  98. }
  99. return r;
  100. }
  101. struct rtp_payload_encode_t *rtp_ts_encode()
  102. {
  103. static struct rtp_payload_encode_t encode = {
  104. rtp_ts_pack_create,
  105. rtp_ts_pack_destroy,
  106. rtp_ts_pack_get_info,
  107. rtp_ts_pack_input,
  108. };
  109. return &encode;
  110. }