rtppacketizationconfig.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * Copyright (c) 2020 Filip Klembara (in2core)
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #if RTC_ENABLE_MEDIA
  19. #include "rtppacketizationconfig.hpp"
  20. #include <cassert>
  21. namespace rtc {
  22. RtpPacketizationConfig::RtpPacketizationConfig(SSRC ssrc, string cname, uint8_t payloadType,
  23. uint32_t clockRate,
  24. optional<uint16_t> sequenceNumber,
  25. optional<uint32_t> timestamp)
  26. : ssrc(ssrc), cname(cname), payloadType(payloadType), clockRate(clockRate) {
  27. assert(clockRate > 0);
  28. srand((unsigned)time(NULL));
  29. if (sequenceNumber.has_value()) {
  30. this->sequenceNumber = sequenceNumber.value();
  31. } else {
  32. this->sequenceNumber = rand();
  33. }
  34. if (timestamp.has_value()) {
  35. this->timestamp = timestamp.value();
  36. } else {
  37. this->timestamp = rand();
  38. }
  39. this->_startTimestamp = this->timestamp;
  40. }
  41. void RtpPacketizationConfig::setStartTime(double startTime_s, EpochStart epochStart,
  42. optional<uint32_t> startTimestamp) {
  43. this->_startTime_s = startTime_s + static_cast<unsigned long long>(epochStart);
  44. if (startTimestamp.has_value()) {
  45. this->_startTimestamp = startTimestamp.value();
  46. timestamp = this->startTimestamp;
  47. } else {
  48. this->_startTimestamp = timestamp;
  49. }
  50. }
  51. double RtpPacketizationConfig::getSecondsFromTimestamp(uint32_t timestamp, uint32_t clockRate) {
  52. return double(timestamp) / double(clockRate);
  53. }
  54. double RtpPacketizationConfig::timestampToSeconds(uint32_t timestamp) {
  55. return RtpPacketizationConfig::getSecondsFromTimestamp(timestamp, clockRate);
  56. }
  57. uint32_t RtpPacketizationConfig::getTimestampFromSeconds(double seconds, uint32_t clockRate) {
  58. return uint32_t(seconds * clockRate);
  59. }
  60. uint32_t RtpPacketizationConfig::secondsToTimestamp(double seconds) {
  61. return RtpPacketizationConfig::getTimestampFromSeconds(seconds, clockRate);
  62. }
  63. } // namespace rtc
  64. #endif /* RTC_ENABLE_MEDIA */