RtpExt.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
  5. *
  6. * Use of this source code is governed by MIT-like license that can be found in the
  7. * LICENSE file in the root of the source tree. All contributing project authors
  8. * may be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef ZLMEDIAKIT_RTPEXT_H
  11. #define ZLMEDIAKIT_RTPEXT_H
  12. #include <stdint.h>
  13. #include <map>
  14. #include <string>
  15. #include "Common/macros.h"
  16. #include "Rtsp/Rtsp.h"
  17. namespace mediakit {
  18. #define RTP_EXT_MAP(XX) \
  19. XX(ssrc_audio_level, "urn:ietf:params:rtp-hdrext:ssrc-audio-level") \
  20. XX(abs_send_time, "http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time") \
  21. XX(transport_cc, "http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01") \
  22. XX(sdes_mid, "urn:ietf:params:rtp-hdrext:sdes:mid") \
  23. XX(sdes_rtp_stream_id, "urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id") \
  24. XX(sdes_repaired_rtp_stream_id, "urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id") \
  25. XX(video_timing, "http://www.webrtc.org/experiments/rtp-hdrext/video-timing") \
  26. XX(color_space, "http://www.webrtc.org/experiments/rtp-hdrext/color-space") \
  27. XX(csrc_audio_level, "urn:ietf:params:rtp-hdrext:csrc-audio-level") \
  28. XX(framemarking, "http://tools.ietf.org/html/draft-ietf-avtext-framemarking-07") \
  29. XX(video_content_type, "http://www.webrtc.org/experiments/rtp-hdrext/video-content-type") \
  30. XX(playout_delay, "http://www.webrtc.org/experiments/rtp-hdrext/playout-delay") \
  31. XX(video_orientation, "urn:3gpp:video-orientation") \
  32. XX(toffset, "urn:ietf:params:rtp-hdrext:toffset") \
  33. XX(av1, "https://aomediacodec.github.io/av1-rtp-spec/#dependency-descriptor-rtp-header-extension") \
  34. XX(encrypt, "urn:ietf:params:rtp-hdrext:encrypt")
  35. enum class RtpExtType : uint8_t {
  36. padding = 0,
  37. #define XX(type, uri) type,
  38. RTP_EXT_MAP(XX)
  39. #undef XX
  40. reserved = 15,
  41. };
  42. class RtcMedia;
  43. // 使用次对象的方法前需保证RtpHeader内存未释放 [AUTO-TRANSLATED:0378877f]
  44. // Ensure that the RtpHeader memory has not been released before using the methods of this object
  45. class RtpExt {
  46. public:
  47. template<typename Type>
  48. friend void appendExt(std::map<uint8_t, RtpExt> &ret, uint8_t *ptr, const uint8_t *end);
  49. friend class RtpExtContext;
  50. static std::map<uint8_t/*id*/, RtpExt/*data*/> getExtValue(const RtpHeader *header);
  51. static RtpExtType getExtType(const std::string &url);
  52. static const std::string& getExtUrl(RtpExtType type);
  53. static const char *getExtName(RtpExtType type);
  54. void setType(RtpExtType type);
  55. RtpExtType getType() const;
  56. std::string dumpString() const;
  57. uint8_t getAudioLevel(bool *vad) const;
  58. uint32_t getAbsSendTime() const;
  59. uint16_t getTransportCCSeq() const;
  60. std::string getSdesMid() const;
  61. std::string getRtpStreamId() const;
  62. std::string getRepairedRtpStreamId() const;
  63. void getVideoTiming(uint8_t &flags,
  64. uint16_t &encode_start,
  65. uint16_t &encode_finish,
  66. uint16_t &packetization_complete,
  67. uint16_t &last_pkt_left_pacer,
  68. uint16_t &reserved_net0,
  69. uint16_t &reserved_net1) const;
  70. uint8_t getVideoContentType() const;
  71. void getVideoOrientation(bool &camera_bit,
  72. bool &flip_bit,
  73. bool &first_rotation,
  74. bool &second_rotation) const;
  75. void getPlayoutDelay(uint16_t &min_delay, uint16_t &max_delay) const;
  76. uint32_t getTransmissionOffset() const;
  77. uint8_t getFramemarkingTID() const;
  78. void setExtId(uint8_t ext_id);
  79. void clearExt();
  80. operator bool () const;
  81. private:
  82. RtpExt() = default;
  83. RtpExt(void *ptr, bool one_byte_ext, const char *str, size_t size);
  84. const char *data() const;
  85. size_t size() const;
  86. const uint8_t& operator[](size_t pos) const;
  87. operator std::string() const;
  88. private:
  89. void *_ext = nullptr;
  90. const char *_data;
  91. size_t _size;
  92. bool _one_byte_ext = true;
  93. RtpExtType _type = RtpExtType::padding;
  94. };
  95. class RtcMedia;
  96. class RtpExtContext {
  97. public:
  98. using Ptr = std::shared_ptr<RtpExtContext>;
  99. using OnGetRtp = std::function<void(uint8_t pt, uint32_t ssrc, const std::string &rid)>;
  100. RtpExtContext(const RtcMedia &media);
  101. void setOnGetRtp(OnGetRtp cb);
  102. std::string getRid(uint32_t ssrc) const;
  103. void setRid(uint32_t ssrc, const std::string &rid);
  104. RtpExt changeRtpExtId(const RtpHeader *header, bool is_recv, std::string *rid_ptr = nullptr, RtpExtType type = RtpExtType::padding);
  105. private:
  106. void onGetRtp(uint8_t pt, uint32_t ssrc, const std::string &rid);
  107. private:
  108. OnGetRtp _cb;
  109. // 发送rtp时需要修改rtp ext id [AUTO-TRANSLATED:b92a494b]
  110. // Modify the rtp ext id when sending rtp
  111. std::map<RtpExtType, uint8_t> _rtp_ext_type_to_id;
  112. // 接收rtp时需要修改rtp ext id [AUTO-TRANSLATED:685e7a01]
  113. // Modify the rtp ext id when receiving rtp
  114. std::unordered_map<uint8_t, RtpExtType> _rtp_ext_id_to_type;
  115. //ssrc --> rid
  116. std::unordered_map<uint32_t/*simulcast ssrc*/, std::string/*rid*/> _ssrc_to_rid;
  117. };
  118. } //namespace mediakit
  119. #endif //ZLMEDIAKIT_RTPEXT_H