rtppacketizer.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * Copyright (c) 2020 Filip Klembara (in2core)
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. #ifndef RTC_RTP_PACKETIZER_H
  9. #define RTC_RTP_PACKETIZER_H
  10. #if RTC_ENABLE_MEDIA
  11. #include "mediahandler.hpp"
  12. #include "message.hpp"
  13. #include "rtppacketizationconfig.hpp"
  14. namespace rtc {
  15. /// RTP packetizer
  16. class RTC_CPP_EXPORT RtpPacketizer : public MediaHandler {
  17. public:
  18. /// Default maximum fragment size (for video packetizers)
  19. inline static const size_t DefaultMaxFragmentSize = RTC_DEFAULT_MAX_FRAGMENT_SIZE;
  20. /// Clock rate for video in RTP
  21. inline static const uint32_t VideoClockRate = 90 * 1000;
  22. /// Constructs packetizer with given RTP configuration
  23. /// @note RTP configuration is used in packetization process which may change some configuration
  24. /// properties such as sequence number.
  25. /// @param rtpConfig RTP configuration
  26. RtpPacketizer(shared_ptr<RtpPacketizationConfig> rtpConfig);
  27. virtual ~RtpPacketizer();
  28. virtual void media(const Description::Media &desc) override;
  29. virtual void outgoing(message_vector &messages, const message_callback &send) override;
  30. /// RTP packetization config
  31. const shared_ptr<RtpPacketizationConfig> rtpConfig;
  32. protected:
  33. /// Fragment data into payloads
  34. /// Default implementation returns data as a single payload
  35. /// @param message Input data
  36. virtual std::vector<binary> fragment(binary data);
  37. /// Creates an RTP packet for a payload
  38. /// @note This function increases the sequence number.
  39. /// @param payload RTP payload
  40. /// @param mark Set marker flag in RTP packet if true
  41. virtual message_ptr packetize(const binary &payload, bool mark);
  42. // For backward compatibility, do not use
  43. [[deprecated]] virtual message_ptr packetize(shared_ptr<binary> payload, bool mark);
  44. private:
  45. static const auto RtpHeaderSize = 12;
  46. static const auto RtpExtHeaderCvoSize = 8;
  47. };
  48. // Generic audio RTP packetizer
  49. template <uint32_t DEFAULT_CLOCK_RATE>
  50. class RTC_CPP_EXPORT AudioRtpPacketizer final : public RtpPacketizer {
  51. public:
  52. inline static const uint32_t DefaultClockRate = DEFAULT_CLOCK_RATE;
  53. inline static const uint32_t defaultClockRate [[deprecated("Use DefaultClockRate")]] =
  54. DEFAULT_CLOCK_RATE; // for backward compatibility
  55. AudioRtpPacketizer(shared_ptr<RtpPacketizationConfig> rtpConfig)
  56. : RtpPacketizer(std::move(rtpConfig)) {}
  57. };
  58. // Audio RTP packetizers
  59. using OpusRtpPacketizer = AudioRtpPacketizer<48000>;
  60. using AACRtpPacketizer = AudioRtpPacketizer<48000>;
  61. using PCMARtpPacketizer = AudioRtpPacketizer<8000>;
  62. using PCMURtpPacketizer = AudioRtpPacketizer<8000>;
  63. // Dummy wrapper for backward compatibility, do not use
  64. class RTC_CPP_EXPORT PacketizationHandler final : public MediaHandler {
  65. public:
  66. PacketizationHandler(shared_ptr<RtpPacketizer> packetizer)
  67. : mPacketizer(std::move(packetizer)) {}
  68. inline void outgoing(message_vector &messages, const message_callback &send) {
  69. return mPacketizer->outgoing(messages, send);
  70. }
  71. private:
  72. shared_ptr<RtpPacketizer> mPacketizer;
  73. };
  74. // Audio packetization handlers for backward compatibility, do not use
  75. using OpusPacketizationHandler [[deprecated("Add OpusRtpPacketizer directly")]] =
  76. PacketizationHandler;
  77. using AACPacketizationHandler [[deprecated("Add AACRtpPacketizer directly")]] =
  78. PacketizationHandler;
  79. } // namespace rtc
  80. #endif /* RTC_ENABLE_MEDIA */
  81. #endif /* RTC_RTP_PACKETIZER_H */