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.

135 lignes
3.3KB

  1. #include "rtp-payload-helper.h"
  2. #include "rtp-param.h"
  3. #include <assert.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <errno.h>
  7. void* rtp_payload_helper_create(struct rtp_payload_t *handler, void* cbparam)
  8. {
  9. struct rtp_payload_helper_t *helper;
  10. helper = (struct rtp_payload_helper_t *)calloc(1, sizeof(*helper));
  11. if (!helper)
  12. return NULL;
  13. memcpy(&helper->handler, handler, sizeof(helper->handler));
  14. helper->maxsize = RTP_PAYLOAD_MAX_SIZE;
  15. helper->cbparam = cbparam;
  16. helper->__flags = -1;
  17. return helper;
  18. }
  19. void rtp_payload_helper_destroy(void* p)
  20. {
  21. struct rtp_payload_helper_t *helper;
  22. helper = (struct rtp_payload_helper_t *)p;
  23. if (helper->ptr)
  24. free(helper->ptr);
  25. #if defined(_DEBUG) || defined(DEBUG)
  26. memset(helper, 0xCC, sizeof(*helper));
  27. #endif
  28. free(helper);
  29. }
  30. int rtp_payload_check(struct rtp_payload_helper_t* helper, const struct rtp_packet_t* pkt)
  31. {
  32. int lost; // next frame lost packet flags
  33. // first packet only
  34. if (-1 == helper->__flags)
  35. {
  36. // TODO: first packet lost ???
  37. helper->__flags = 0;
  38. helper->seq = (uint16_t)(pkt->rtp.seq - 1); // disable packet lost
  39. helper->timestamp = pkt->rtp.timestamp + 1; // flag for new frame
  40. }
  41. lost = 0;
  42. // check sequence number
  43. if ((uint16_t)pkt->rtp.seq != (uint16_t)(helper->seq + 1))
  44. {
  45. lost = 1;
  46. //helper->size = 0;
  47. helper->lost = 1;
  48. //helper->flags |= RTP_PAYLOAD_FLAG_PACKET_LOST;
  49. //helper->timestamp = pkt->rtp.timestamp;
  50. }
  51. helper->seq = (uint16_t)pkt->rtp.seq;
  52. // check timestamp
  53. if (pkt->rtp.timestamp != helper->timestamp)
  54. {
  55. rtp_payload_onframe(helper);
  56. // lost:
  57. // 0 - packet lost before timestamp change
  58. // 1 - packet lost on timestamp changed, can't known losted packet is at old packet tail or new packet start, so two packets mark as packet lost
  59. if(0 != lost)
  60. helper->lost = lost;
  61. }
  62. helper->timestamp = pkt->rtp.timestamp;
  63. return 0;
  64. }
  65. int rtp_payload_write(struct rtp_payload_helper_t* helper, const struct rtp_packet_t* pkt)
  66. {
  67. int size;
  68. size = helper->size + pkt->payloadlen;
  69. if (size > helper->maxsize || size < 0)
  70. return -EINVAL;
  71. if (size > helper->capacity)
  72. {
  73. void *ptr;
  74. size += size / 4 > 16000 ? size / 4 : 16000;
  75. ptr = realloc(helper->ptr, size);
  76. if (!ptr)
  77. {
  78. //helper->flags |= RTP_PAYLOAD_FLAG_PACKET_LOST;
  79. helper->lost = 1;
  80. //helper->size = 0;
  81. return -ENOMEM;
  82. }
  83. helper->ptr = (uint8_t*)ptr;
  84. helper->capacity = size;
  85. }
  86. assert(helper->capacity >= helper->size + pkt->payloadlen);
  87. memcpy(helper->ptr + helper->size, pkt->payload, pkt->payloadlen);
  88. helper->size += pkt->payloadlen;
  89. return 0;
  90. }
  91. int rtp_payload_onframe(struct rtp_payload_helper_t *helper)
  92. {
  93. int r;
  94. r = 0;
  95. if (helper->size > 0
  96. #if !defined(RTP_ENABLE_COURRUPT_PACKET)
  97. && 0 == helper->lost
  98. #endif
  99. )
  100. {
  101. // previous packet done
  102. r = helper->handler.packet(helper->cbparam, helper->ptr, helper->size, helper->timestamp, helper->__flags | (helper->lost ? RTP_PAYLOAD_FLAG_PACKET_CORRUPT : 0));
  103. // RTP_PAYLOAD_FLAG_PACKET_LOST: miss
  104. helper->__flags &= ~RTP_PAYLOAD_FLAG_PACKET_LOST; // clear packet lost flag
  105. }
  106. // set packet lost flag on next frame
  107. if(helper->lost)
  108. helper->__flags |= RTP_PAYLOAD_FLAG_PACKET_LOST;
  109. // new frame start
  110. helper->lost = 0;
  111. helper->size = 0;
  112. return r;
  113. }