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.

243 lines
7.4KB

  1. #include "rtp-payload.h"
  2. #include "rtp-profile.h"
  3. #include "rtp-packet.h"
  4. #include "rtp-payload-internal.h"
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <assert.h>
  8. #define TS_PACKET_SIZE 188
  9. #if defined(OS_WINDOWS)
  10. #define strcasecmp _stricmp
  11. #endif
  12. struct rtp_payload_delegate_t
  13. {
  14. struct rtp_payload_encode_t* encoder;
  15. struct rtp_payload_decode_t* decoder;
  16. void* packer;
  17. };
  18. /// @return 0-ok, <0-error
  19. static int rtp_payload_find(int payload, const char* encoding, struct rtp_payload_delegate_t* codec);
  20. void* rtp_payload_encode_create(int payload, const char* name, uint16_t seq, uint32_t ssrc, struct rtp_payload_t *handler, void* cbparam)
  21. {
  22. int size;
  23. struct rtp_payload_delegate_t* ctx;
  24. ctx = calloc(1, sizeof(*ctx));
  25. if (ctx)
  26. {
  27. size = rtp_packet_getsize();
  28. if (rtp_payload_find(payload, name, ctx) < 0
  29. || NULL == (ctx->packer = ctx->encoder->create(size, (uint8_t)payload, seq, ssrc, handler, cbparam)))
  30. {
  31. free(ctx);
  32. return NULL;
  33. }
  34. }
  35. return ctx;
  36. }
  37. void rtp_payload_encode_destroy(void* encoder)
  38. {
  39. struct rtp_payload_delegate_t* ctx;
  40. ctx = (struct rtp_payload_delegate_t*)encoder;
  41. ctx->encoder->destroy(ctx->packer);
  42. free(ctx);
  43. }
  44. void rtp_payload_encode_getinfo(void* encoder, uint16_t* seq, uint32_t* timestamp)
  45. {
  46. struct rtp_payload_delegate_t* ctx;
  47. ctx = (struct rtp_payload_delegate_t*)encoder;
  48. ctx->encoder->get_info(ctx->packer, seq, timestamp);
  49. }
  50. int rtp_payload_encode_input(void* encoder, const void* data, int bytes, uint32_t timestamp)
  51. {
  52. struct rtp_payload_delegate_t* ctx;
  53. ctx = (struct rtp_payload_delegate_t*)encoder;
  54. return ctx->encoder->input(ctx->packer, data, bytes, timestamp);
  55. }
  56. void* rtp_payload_decode_create(int payload, const char* name, struct rtp_payload_t *handler, void* cbparam)
  57. {
  58. struct rtp_payload_delegate_t* ctx;
  59. ctx = calloc(1, sizeof(*ctx));
  60. if (ctx)
  61. {
  62. if (rtp_payload_find(payload, name, ctx) < 0
  63. || NULL == (ctx->packer = ctx->decoder->create(handler, cbparam)))
  64. {
  65. free(ctx);
  66. return NULL;
  67. }
  68. }
  69. return ctx;
  70. }
  71. void rtp_payload_decode_destroy(void* decoder)
  72. {
  73. struct rtp_payload_delegate_t* ctx;
  74. ctx = (struct rtp_payload_delegate_t*)decoder;
  75. ctx->decoder->destroy(ctx->packer);
  76. free(ctx);
  77. }
  78. int rtp_payload_decode_input(void* decoder, const void* packet, int bytes)
  79. {
  80. struct rtp_payload_delegate_t* ctx;
  81. ctx = (struct rtp_payload_delegate_t*)decoder;
  82. return ctx->decoder->input(ctx->packer, packet, bytes);
  83. }
  84. // Default max packet size (1500, minus allowance for IP, UDP, UMTP headers)
  85. // (Also, make it a multiple of 4 bytes, just in case that matters.)
  86. //static int s_max_packet_size = 1456; // from Live555 MultiFrameRTPSink.cpp RTP_PAYLOAD_MAX_SIZE
  87. //static size_t s_max_packet_size = 576; // UNIX Network Programming by W. Richard Stevens
  88. static int s_max_packet_size = 1434; // from VLC
  89. void rtp_packet_setsize(int bytes)
  90. {
  91. s_max_packet_size = bytes < 564 ? 564 : bytes;
  92. }
  93. int rtp_packet_getsize()
  94. {
  95. return s_max_packet_size;
  96. }
  97. static int rtp_payload_find(int payload, const char* encoding, struct rtp_payload_delegate_t* codec)
  98. {
  99. assert(payload >= 0 && payload <= 127);
  100. if (payload >= RTP_PAYLOAD_DYNAMIC && encoding)
  101. {
  102. if (0 == strcasecmp(encoding, "H264"))
  103. {
  104. // H.264 video (MPEG-4 Part 10) (RFC 6184)
  105. codec->encoder = rtp_h264_encode();
  106. codec->decoder = rtp_h264_decode();
  107. }
  108. else if (0 == strcasecmp(encoding, "H265") || 0 == strcasecmp(encoding, "HEVC"))
  109. {
  110. // H.265 video (HEVC) (RFC 7798)
  111. codec->encoder = rtp_h265_encode();
  112. codec->decoder = rtp_h265_decode();
  113. }
  114. else if (0 == strcasecmp(encoding, "MP4V-ES") || 0 == strcasecmp(encoding, "MPEG4"))
  115. {
  116. // RFC6416 RTP Payload Format for MPEG-4 Audio/Visual Streams
  117. // 5. RTP Packetization of MPEG-4 Visual Bitstreams (p8)
  118. // 7.1 Media Type Registration for MPEG-4 Audio/Visual Streams (p17)
  119. codec->encoder = rtp_mp4v_es_encode();
  120. codec->decoder = rtp_mp4v_es_decode();
  121. }
  122. else if (0 == strcasecmp(encoding, "MP4A-LATM"))
  123. {
  124. // RFC6416 RTP Payload Format for MPEG-4 Audio/Visual Streams
  125. // 6. RTP Packetization of MPEG-4 Audio Bitstreams (p15)
  126. // 7.3 Media Type Registration for MPEG-4 Audio (p21)
  127. codec->encoder = rtp_mp4a_latm_encode();
  128. codec->decoder = rtp_mp4a_latm_decode();
  129. }
  130. else if (0 == strcasecmp(encoding, "mpeg4-generic"))
  131. {
  132. /// RFC3640 RTP Payload Format for Transport of MPEG-4 Elementary Streams
  133. /// 4.1. MIME Type Registration (p27)
  134. codec->encoder = rtp_mpeg4_generic_encode();
  135. codec->decoder = rtp_mpeg4_generic_decode();
  136. }
  137. else if (0 == strcasecmp(encoding, "VP8"))
  138. {
  139. /// RFC7741 RTP Payload Format for VP8 Video
  140. /// 6.1. Media Type Definition (p21)
  141. codec->encoder = rtp_vp8_encode();
  142. codec->decoder = rtp_vp8_decode();
  143. }
  144. else if (0 == strcasecmp(encoding, "VP9"))
  145. {
  146. /// RTP Payload Format for VP9 Video draft-ietf-payload-vp9-03
  147. /// 6.1. Media Type Definition (p15)
  148. codec->encoder = rtp_vp9_encode();
  149. codec->decoder = rtp_vp9_decode();
  150. }
  151. else if (0 == strcasecmp(encoding, "AV1"))
  152. {
  153. /// https://aomediacodec.github.io/av1-rtp-spec/#7-payload-format-parameters
  154. codec->encoder = rtp_av1_encode();
  155. codec->decoder = rtp_av1_decode();
  156. }
  157. else if (0 == strcasecmp(encoding, "MP2P") || 0 == strcasecmp(encoding, "PS")) // MPEG-2 Program Streams video (RFC 2250)
  158. {
  159. codec->encoder = rtp_ts_encode();
  160. codec->decoder = rtp_ps_decode();
  161. }
  162. else if (0 == strcasecmp(encoding, "MP1S")) // MPEG-1 Systems Streams video (RFC 2250)
  163. {
  164. codec->encoder = rtp_ts_encode();
  165. codec->decoder = rtp_ts_decode();
  166. }
  167. else if (0 == strcasecmp(encoding, "opus") // RFC7587 RTP Payload Format for the Opus Speech and Audio Codec
  168. || 0 == strcasecmp(encoding, "G726-16") // ITU-T G.726 audio 16 kbit/s (RFC 3551)
  169. || 0 == strcasecmp(encoding, "G726-24") // ITU-T G.726 audio 24 kbit/s (RFC 3551)
  170. || 0 == strcasecmp(encoding, "G726-32") // ITU-T G.726 audio 32 kbit/s (RFC 3551)
  171. || 0 == strcasecmp(encoding, "G726-40") // ITU-T G.726 audio 40 kbit/s (RFC 3551)
  172. || 0 == strcasecmp(encoding, "G7221")) // RFC5577 RTP Payload Format for ITU-T Recommendation G.722.1
  173. {
  174. codec->encoder = rtp_common_encode();
  175. codec->decoder = rtp_common_decode();
  176. }
  177. else
  178. {
  179. return -1;
  180. }
  181. }
  182. else
  183. {
  184. #if defined(_DEBUG) || defined(DEBUG)
  185. const struct rtp_profile_t* profile;
  186. profile = rtp_profile_find(payload);
  187. assert(!profile || !encoding || !*encoding || 0 == strcasecmp(profile->name, encoding));
  188. #endif
  189. switch (payload)
  190. {
  191. case RTP_PAYLOAD_PCMU: // ITU-T G.711 PCM u-Law audio 64 kbit/s (RFC 3551)
  192. case RTP_PAYLOAD_PCMA: // ITU-T G.711 PCM A-Law audio 64 kbit/s (RFC 3551)
  193. case RTP_PAYLOAD_G722: // ITU-T G.722 audio 64 kbit/s (RFC 3551)
  194. case RTP_PAYLOAD_G729: // ITU-T G.729 and G.729a audio 8 kbit/s (RFC 3551)
  195. codec->encoder = rtp_common_encode();
  196. codec->decoder = rtp_common_decode();
  197. break;
  198. case RTP_PAYLOAD_MP3: // MPEG-1 or MPEG-2 audio only (RFC 3551, RFC 2250)
  199. case RTP_PAYLOAD_MPV: // MPEG-1 and MPEG-2 video (RFC 2250)
  200. codec->encoder = rtp_mpeg1or2es_encode();
  201. codec->decoder = rtp_mpeg1or2es_decode();
  202. break;
  203. case RTP_PAYLOAD_MP2T: // MPEG-2 transport stream (RFC 2250)
  204. codec->encoder = rtp_ts_encode();
  205. codec->decoder = rtp_ts_decode();
  206. break;
  207. case RTP_PAYLOAD_AV1X: // https://bugs.chromium.org/p/webrtc/issues/detail?id=11042
  208. codec->encoder = rtp_av1_encode();
  209. codec->decoder = rtp_av1_decode();
  210. break;
  211. case RTP_PAYLOAD_JPEG:
  212. case RTP_PAYLOAD_H263:
  213. return -1; // TODO
  214. default:
  215. return -1; // not support
  216. }
  217. }
  218. return 0;
  219. }