Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

rtp-demuxer.c 6.3KB

il y a 6 mois
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #include "rtp-demuxer.h"
  2. #include "rtp-internal.h"
  3. #include "rtp-payload.h"
  4. #include "rtp-packet.h"
  5. #include "rtp-queue.h"
  6. #include "rtp-param.h"
  7. #include "rtp.h"
  8. #include "rtcp-header.h"
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <assert.h>
  12. #include <stdio.h>
  13. #include <errno.h>
  14. struct rtp_demuxer_t
  15. {
  16. uint32_t ssrc;
  17. uint64_t clock; // rtcp clock
  18. uint8_t* ptr;
  19. int cap, max;
  20. rtp_queue_t* queue;
  21. void* payload;
  22. void* rtp;
  23. rtp_demuxer_onpacket onpkt;
  24. void* param;
  25. };
  26. static int rtp_onpacket(void* param, const void *packet, int bytes, uint32_t timestamp, int flags)
  27. {
  28. struct rtp_demuxer_t* rtp;
  29. rtp = (struct rtp_demuxer_t*)param;
  30. // TODO: rtp timestamp -> pts/dts
  31. return rtp->onpkt ? rtp->onpkt(rtp->param, packet, bytes, timestamp, flags) : -1;
  32. }
  33. static void rtp_on_rtcp(void* param, const struct rtcp_msg_t* msg)
  34. {
  35. //struct rtp_demuxer_t* rtp;
  36. //rtp = (struct rtp_demuxer_t*)param;
  37. if (RTCP_BYE == msg->type)
  38. {
  39. printf("finished\n");
  40. //rtp->onpkt(rtp->param, NULL, 0, 0, 0);
  41. }
  42. }
  43. static struct rtp_packet_t* rtp_demuxer_alloc(struct rtp_demuxer_t* rtp, const void* data, int bytes)
  44. {
  45. int r;
  46. uint8_t* ptr;
  47. struct rtp_packet_t* pkt;
  48. if(rtp->cap < bytes + (int)sizeof(struct rtp_packet_t) + (int)sizeof(int) /*bytes*/ )
  49. {
  50. r = bytes + sizeof(struct rtp_packet_t) + sizeof(int);
  51. r = r > 1500 ? r : 1500;
  52. ptr = (uint8_t*)realloc(rtp->ptr, r + sizeof(int) /*cap*/ );
  53. if(!ptr)
  54. return NULL;
  55. rtp->cap = r;
  56. rtp->ptr = ptr;
  57. *(int*)ptr = r; /*cap*/
  58. }
  59. *((int*)rtp->ptr + 1) = bytes; /*bytes*/
  60. pkt = (struct rtp_packet_t*)(rtp->ptr + sizeof(int) /*cap*/ + sizeof(int) /*bytes*/ );
  61. memcpy(pkt + 1, data, bytes);
  62. r = rtp_packet_deserialize(pkt, pkt + 1, bytes);
  63. if(0 != r)
  64. return NULL;
  65. rtp->cap = 0; // need more memory
  66. rtp->ptr = NULL;
  67. return pkt;
  68. }
  69. static void rtp_demuxer_freepkt(void* param, struct rtp_packet_t* pkt)
  70. {
  71. int cap;
  72. uint8_t* ptr;
  73. struct rtp_demuxer_t* rtp;
  74. rtp = (struct rtp_demuxer_t*)param;
  75. ptr = (uint8_t*)pkt - sizeof(int) /*cap*/ - sizeof(int) /*bytes*/ ;
  76. cap = *(int*)ptr;
  77. if(cap <= rtp->cap)
  78. {
  79. free(ptr);
  80. return;
  81. }
  82. if(rtp->cap > 0 && rtp->ptr)
  83. free(rtp->ptr);
  84. rtp->cap = cap;
  85. rtp->ptr = ptr;
  86. }
  87. static int rtp_demuxer_init(struct rtp_demuxer_t* rtp, int jitter, int frequency, int payload, const char* encoding)
  88. {
  89. uint32_t timestamp;
  90. struct rtp_event_t evthandler;
  91. struct rtp_payload_t handler;
  92. // const struct rtp_profile_t* profile;
  93. // profile = rtp_profile_find(payload);
  94. // frequency = profile ? profile->frequency : 90000;
  95. memset(&handler, 0, sizeof(handler));
  96. handler.alloc = NULL;
  97. handler.free = NULL;
  98. handler.packet = rtp_onpacket;
  99. rtp->payload = rtp_payload_decode_create(payload, encoding, &handler, rtp);
  100. timestamp = (uint32_t)rtpclock();
  101. evthandler.on_rtcp = rtp_on_rtcp;
  102. rtp->rtp = rtp_create(&evthandler, rtp, rtp->ssrc, timestamp, frequency ? frequency : 90000, 2 * 1024 * 1024, 0);
  103. rtp->queue = rtp_queue_create(jitter, frequency, rtp_demuxer_freepkt, rtp);
  104. return rtp->payload && rtp->rtp && rtp->queue? 0 : -1;
  105. }
  106. struct rtp_demuxer_t* rtp_demuxer_create(int jitter, int frequency, int payload, const char* encoding, rtp_demuxer_onpacket onpkt, void* param)
  107. {
  108. struct rtp_demuxer_t* rtp;
  109. rtp = (struct rtp_demuxer_t*)calloc(1, sizeof(*rtp));
  110. if(!rtp)
  111. return NULL;
  112. if(0 != rtp_demuxer_init(rtp, jitter, frequency, payload, encoding))
  113. {
  114. rtp_demuxer_destroy(&rtp);
  115. return NULL;
  116. }
  117. rtp->onpkt = onpkt;
  118. rtp->param = param;
  119. rtp->clock = rtpclock();
  120. rtp->ssrc = rtp_ssrc();
  121. rtp->max = RTP_PAYLOAD_MAX_SIZE;
  122. return rtp;
  123. }
  124. int rtp_demuxer_destroy(struct rtp_demuxer_t** pprtp)
  125. {
  126. struct rtp_demuxer_t* rtp;
  127. if(pprtp && *pprtp)
  128. {
  129. rtp = *pprtp;
  130. if(rtp->rtp)
  131. rtp_destroy(rtp->rtp);
  132. if(rtp->payload)
  133. rtp_payload_decode_destroy(rtp->payload);
  134. if(rtp->queue)
  135. rtp_queue_destroy(rtp->queue);
  136. if(rtp->ptr)
  137. free(rtp->ptr);
  138. free(rtp);
  139. }
  140. return 0;
  141. }
  142. int rtp_demuxer_input(struct rtp_demuxer_t* rtp, const void* data, int bytes)
  143. {
  144. int r;
  145. uint8_t pt;
  146. struct rtp_packet_t* pkt;
  147. if (bytes < 12 || bytes > rtp->max)
  148. return -EINVAL;
  149. pt = ((uint8_t*)data)[1];
  150. // RFC7983 SRTP: https://tools.ietf.org/html/draft-ietf-avtcore-rfc5764-mux-fixes
  151. // http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml#rtp-parameters-4
  152. // RFC 5761 (RTCP-mux) states this range for secure RTCP/RTP detection.
  153. // RTCP packet types in the ranges 1-191 and 224-254 SHOULD only be used when other values have been exhausted.
  154. if(pt < RTCP_FIR || pt > RTCP_LIMIT)
  155. {
  156. pkt = rtp_demuxer_alloc(rtp, data, bytes);
  157. if (!pkt)
  158. return -ENOMEM;
  159. r = rtp_queue_write(rtp->queue, pkt);
  160. if(r <= 0) // 0-discard packet(duplicate/too late)
  161. {
  162. rtp_demuxer_freepkt(rtp, pkt);
  163. return r;
  164. }
  165. // re-order packet
  166. pkt = rtp_queue_read(rtp->queue);
  167. while(pkt)
  168. {
  169. bytes = *(int*)((uint8_t*)pkt - sizeof(int) /*bytes*/ );
  170. r = rtp_onreceived(rtp->rtp, pkt + 1, bytes);
  171. r = rtp_payload_decode_input(rtp->payload, pkt + 1, bytes);
  172. rtp_demuxer_freepkt(rtp, pkt);
  173. if(r < 0)
  174. return r;
  175. pkt = rtp_queue_read(rtp->queue);
  176. }
  177. }
  178. else
  179. {
  180. r = rtp_onreceived_rtcp(rtp->rtp, data, bytes);
  181. (void)r; // ignore rtcp handler
  182. return pt; // rtcp message type
  183. }
  184. return 0;
  185. }
  186. int rtp_demuxer_rtcp(struct rtp_demuxer_t* rtp, void* buf, int len)
  187. {
  188. int r;
  189. int interval;
  190. uint64_t clock;
  191. r = 0;
  192. clock = rtpclock();
  193. interval = rtp_rtcp_interval(rtp->rtp);
  194. if (rtp->clock + (uint64_t)interval * 1000 < clock)
  195. {
  196. // RTCP report
  197. r = rtp_rtcp_report(rtp->rtp, buf, len);
  198. rtp->clock = clock;
  199. }
  200. return r;
  201. }