rtppacketizationconfig.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. #if RTC_ENABLE_MEDIA
  9. #include "rtppacketizationconfig.hpp"
  10. #include "impl/utils.hpp"
  11. #include <cassert>
  12. #include <cmath>
  13. #include <limits>
  14. #include <random>
  15. namespace rtc {
  16. namespace utils = impl::utils;
  17. RtpPacketizationConfig::RtpPacketizationConfig(SSRC ssrc, string cname, uint8_t payloadType,
  18. uint32_t clockRate, uint8_t videoOrientationId)
  19. : ssrc(ssrc), cname(cname), payloadType(payloadType), clockRate(clockRate),
  20. videoOrientationId(videoOrientationId) {
  21. assert(clockRate > 0);
  22. // RFC 3550: The initial value of the sequence number SHOULD be random (unpredictable) to make
  23. // known-plaintext attacks on encryption more difficult [...] The initial value of the timestamp
  24. // SHOULD be random, as for the sequence number.
  25. auto uniform = std::bind(std::uniform_int_distribution<uint32_t>(), utils::random_engine());
  26. sequenceNumber = static_cast<uint16_t>(uniform());
  27. timestamp = startTimestamp = uniform();
  28. }
  29. double RtpPacketizationConfig::getSecondsFromTimestamp(uint32_t timestamp, uint32_t clockRate) {
  30. return double(timestamp) / double(clockRate);
  31. }
  32. double RtpPacketizationConfig::timestampToSeconds(uint32_t timestamp) {
  33. return RtpPacketizationConfig::getSecondsFromTimestamp(timestamp, clockRate);
  34. }
  35. uint32_t RtpPacketizationConfig::getTimestampFromSeconds(double seconds, uint32_t clockRate) {
  36. return uint32_t(int64_t(round(seconds * double(clockRate)))); // convert to integer then cast to u32
  37. }
  38. uint32_t RtpPacketizationConfig::secondsToTimestamp(double seconds) {
  39. return RtpPacketizationConfig::getTimestampFromSeconds(seconds, clockRate);
  40. }
  41. } // namespace rtc
  42. #endif /* RTC_ENABLE_MEDIA */