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-pack.c 2.6KB

6 kuukautta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // RFC3551 RTP Profile for Audio and Video Conferences with Minimal Control
  2. #include "rtp-packet.h"
  3. #include "rtp-payload-internal.h"
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <assert.h>
  7. #include <errno.h>
  8. struct rtp_packer_t
  9. {
  10. struct rtp_payload_t handler;
  11. void* cbparam;
  12. struct rtp_packet_t pkt;
  13. int size;
  14. };
  15. static void* rtp_pack_create(int size, uint8_t pt, uint16_t seq, uint32_t ssrc, struct rtp_payload_t *handler, void* param)
  16. {
  17. struct rtp_packer_t *packer;
  18. packer = (struct rtp_packer_t *)calloc(1, sizeof(*packer));
  19. if (!packer) return NULL;
  20. memcpy(&packer->handler, handler, sizeof(packer->handler));
  21. packer->cbparam = param;
  22. packer->size = size;
  23. packer->pkt.rtp.v = RTP_VERSION;
  24. packer->pkt.rtp.pt = pt;
  25. packer->pkt.rtp.seq = seq;
  26. packer->pkt.rtp.ssrc = ssrc;
  27. return packer;
  28. }
  29. void rtp_pack_destroy(void* p)
  30. {
  31. struct rtp_packer_t *packer;
  32. packer = (struct rtp_packer_t *)p;
  33. #if defined(_DEBUG) || defined(DEBUG)
  34. memset(packer, 0xCC, sizeof(*packer));
  35. #endif
  36. free(packer);
  37. }
  38. void rtp_pack_get_info(void* p, uint16_t* seq, uint32_t* timestamp)
  39. {
  40. struct rtp_packer_t *packer;
  41. packer = (struct rtp_packer_t *)p;
  42. *seq = (uint16_t)packer->pkt.rtp.seq;
  43. *timestamp = packer->pkt.rtp.timestamp;
  44. }
  45. int rtp_pack_input(void* p, const void* data, int bytes, uint32_t timestamp)
  46. {
  47. int r, n;
  48. uint8_t *rtp;
  49. const uint8_t *ptr;
  50. struct rtp_packer_t *packer;
  51. r = 0;
  52. packer = (struct rtp_packer_t *)p;
  53. assert(packer->pkt.rtp.timestamp != timestamp || !packer->pkt.payload /*first packet*/);
  54. packer->pkt.rtp.timestamp = timestamp; // (uint32_t)time * packer->frequency / 1000; // ms -> 8KHZ
  55. packer->pkt.rtp.m = 0; // marker bit alway 0
  56. for(ptr = (const uint8_t *)data; 0 == r && bytes > 0; ++packer->pkt.rtp.seq)
  57. {
  58. packer->pkt.payload = ptr;
  59. packer->pkt.payloadlen = (bytes + RTP_FIXED_HEADER) <= packer->size ? bytes : (packer->size - RTP_FIXED_HEADER);
  60. ptr += packer->pkt.payloadlen;
  61. bytes -= packer->pkt.payloadlen;
  62. n = RTP_FIXED_HEADER + packer->pkt.payloadlen;
  63. rtp = (uint8_t*)packer->handler.alloc(packer->cbparam, n);
  64. if (!rtp) return -ENOMEM;
  65. n = rtp_packet_serialize(&packer->pkt, rtp, n);
  66. if (n != RTP_FIXED_HEADER + packer->pkt.payloadlen)
  67. {
  68. assert(0);
  69. return -1;
  70. }
  71. r = packer->handler.packet(packer->cbparam, rtp, n, packer->pkt.rtp.timestamp, 0);
  72. packer->handler.free(packer->cbparam, rtp);
  73. //packer->pkt.rtp.timestamp += packer->pkt.payloadlen * packer->frequency / 1000;
  74. }
  75. return r;
  76. }
  77. struct rtp_payload_encode_t *rtp_common_encode()
  78. {
  79. static struct rtp_payload_encode_t packer = {
  80. rtp_pack_create,
  81. rtp_pack_destroy,
  82. rtp_pack_get_info,
  83. rtp_pack_input,
  84. };
  85. return &packer;
  86. }