rtp.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #ifndef RTP_H_
  2. #define RTP_H_
  3. #include <stdint.h>
  4. #ifdef __BYTE_ORDER
  5. #define __BIG_ENDIAN 4321
  6. #define __LITTLE_ENDIAN 1234
  7. #elif __APPLE__
  8. #include <machine/endian.h>
  9. #else
  10. #include <endian.h>
  11. #endif
  12. #include "config.h"
  13. #include "peer_connection.h"
  14. typedef enum RtpPayloadType {
  15. PT_PCMU = 0,
  16. PT_PCMA = 8,
  17. PT_G722 = 9,
  18. PT_H264 = 96,
  19. PT_OPUS = 111
  20. } RtpPayloadType;
  21. typedef enum RtpSsrc {
  22. SSRC_H264 = 1,
  23. SSRC_PCMA = 4,
  24. SSRC_PCMU = 5,
  25. SSRC_OPUS = 6,
  26. } RtpSsrc;
  27. typedef struct RtpHeader {
  28. #if __BYTE_ORDER == __BIG_ENDIAN
  29. uint16_t version : 2;
  30. uint16_t padding : 1;
  31. uint16_t extension : 1;
  32. uint16_t csrccount : 4;
  33. uint16_t markerbit : 1;
  34. uint16_t type : 7;
  35. #elif __BYTE_ORDER == __LITTLE_ENDIAN
  36. uint16_t csrccount : 4;
  37. uint16_t extension : 1;
  38. uint16_t padding : 1;
  39. uint16_t version : 2;
  40. uint16_t type : 7;
  41. uint16_t markerbit : 1;
  42. #endif
  43. uint16_t seq_number;
  44. uint32_t timestamp;
  45. uint32_t ssrc;
  46. uint32_t csrc[0];
  47. } RtpHeader;
  48. typedef struct RtpPacket {
  49. RtpHeader header;
  50. uint8_t payload[0];
  51. } RtpPacket;
  52. typedef struct RtpMap {
  53. int pt_h264;
  54. int pt_opus;
  55. int pt_pcma;
  56. } RtpMap;
  57. typedef struct RtpEncoder RtpEncoder;
  58. typedef struct RtpDecoder RtpDecoder;
  59. typedef void (*RtpOnPacket)(uint8_t* packet, size_t bytes, void* user_data);
  60. struct RtpDecoder {
  61. RtpPayloadType type;
  62. RtpOnPacket on_packet;
  63. int (*decode_func)(RtpDecoder* rtp_decoder, uint8_t* data, size_t size);
  64. void* user_data;
  65. };
  66. struct RtpEncoder {
  67. RtpPayloadType type;
  68. RtpOnPacket on_packet;
  69. int (*encode_func)(RtpEncoder* rtp_encoder, uint8_t* data, size_t size);
  70. void* user_data;
  71. uint16_t seq_number;
  72. uint32_t ssrc;
  73. uint32_t timestamp;
  74. uint32_t timestamp_increment;
  75. uint8_t buf[CONFIG_MTU + 128];
  76. };
  77. int rtp_packet_validate(uint8_t* packet, size_t size);
  78. void rtp_encoder_init(RtpEncoder* rtp_encoder, MediaCodec codec, RtpOnPacket on_packet, void* user_data);
  79. int rtp_encoder_encode(RtpEncoder* rtp_encoder, const uint8_t* data, size_t size);
  80. void rtp_decoder_init(RtpDecoder* rtp_decoder, MediaCodec codec, RtpOnPacket on_packet, void* user_data);
  81. int rtp_decoder_decode(RtpDecoder* rtp_decoder, const uint8_t* data, size_t size);
  82. uint32_t rtp_get_ssrc(uint8_t* packet);
  83. #endif // RTP_H_