rtppacketizationconfig.hpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * libdatachannel streamer example
  3. * Copyright (c) 2020 Filip Klembara (in2core)
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef RTPPacketizationConfig_hpp
  19. #define RTPPacketizationConfig_hpp
  20. #if RTC_ENABLE_MEDIA
  21. #include "rtp.hpp"
  22. namespace rtc {
  23. /// RTP configuration used in packetization process
  24. struct RTPPacketizationConfig {
  25. private:
  26. uint32_t _startTimestamp = 0;
  27. double _startTime_s = 0;
  28. RTPPacketizationConfig(const RTPPacketizationConfig&) = delete;
  29. public:
  30. const SSRC ssrc;
  31. const std::string cname;
  32. const uint8_t payloadType;
  33. const uint32_t clockRate;
  34. const double & startTime_s = _startTime_s;
  35. const uint32_t & startTimestamp = _startTimestamp;
  36. /// current sequence number
  37. uint16_t sequenceNumber;
  38. /// current timestamp
  39. uint32_t timestamp;
  40. enum class EpochStart: unsigned long long {
  41. T1970 = 2208988800, // number of seconds between 1970 and 1900
  42. T1900 = 0
  43. };
  44. /// Creates relation between time and timestamp mapping given start time and start timestamp
  45. /// @param startTime_s Start time of the stream
  46. /// @param epochStart Type of used epoch
  47. /// @param startTimestamp Corresponding timestamp for given start time (current timestamp will be used if value is nullopt)
  48. void setStartTime(double startTime_s, EpochStart epochStart, std::optional<uint32_t> startTimestamp = std::nullopt);
  49. /// Construct RTP configuration used in packetization process
  50. /// @param ssrc SSRC of source
  51. /// @param cname CNAME of source
  52. /// @param payloadType Payload type of source
  53. /// @param clockRate Clock rate of source used in timestamps
  54. /// @param sequenceNumber Initial sequence number of RTP packets (random number is choosed if nullopt)
  55. /// @param timestamp Initial timastamp of RTP packets (random number is choosed if nullopt)
  56. RTPPacketizationConfig(SSRC ssrc, std::string cname, uint8_t payloadType, uint32_t clockRate, std::optional<uint16_t> sequenceNumber = std::nullopt, std::optional<uint32_t> timestamp = std::nullopt);
  57. /// Convert timestamp to seconds
  58. /// @param timestamp Timestamp
  59. /// @param clockRate Clock rate for timestamp calculation
  60. static double getSecondsFromTimestamp(uint32_t timestamp, uint32_t clockRate);
  61. /// Convert timestamp to seconds
  62. /// @param timestamp Timestamp
  63. double timestampToSeconds(uint32_t timestamp);
  64. /// Convert seconds to timestamp
  65. /// @param seconds Number of seconds
  66. /// @param clockRate Clock rate for timestamp calculation
  67. static uint32_t getTimestampFromSeconds(double seconds, uint32_t clockRate);
  68. /// Convert seconds to timestamp
  69. /// @param seconds Number of seconds
  70. uint32_t secondsToTimestamp(double seconds);
  71. };
  72. } // namespace
  73. #endif /* RTC_ENABLE_MEDIA */
  74. #endif /* RTPPacketizationConfig_hpp */