rtppacketizationconfig.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. uint8_t videoOrientationId)
  27. : ssrc(ssrc), cname(cname), payloadType(payloadType), clockRate(clockRate), videoOrientationId(videoOrientationId) {
  28. assert(clockRate > 0);
  29. srand((unsigned)time(NULL));
  30. if (sequenceNumber.has_value()) {
  31. this->sequenceNumber = sequenceNumber.value();
  32. } else {
  33. this->sequenceNumber = rand();
  34. }
  35. if (timestamp.has_value()) {
  36. this->timestamp = timestamp.value();
  37. } else {
  38. this->timestamp = rand();
  39. }
  40. this->mStartTimestamp = this->timestamp;
  41. }
  42. void RtpPacketizationConfig::setStartTime(double startTime, EpochStart epochStart,
  43. optional<uint32_t> startTimestamp) {
  44. this->mStartTime = startTime + double(static_cast<uint64_t>(epochStart));
  45. if (startTimestamp.has_value()) {
  46. this->mStartTimestamp = startTimestamp.value();
  47. timestamp = this->startTimestamp;
  48. } else {
  49. this->mStartTimestamp = timestamp;
  50. }
  51. }
  52. double RtpPacketizationConfig::getSecondsFromTimestamp(uint32_t timestamp, uint32_t clockRate) {
  53. return double(timestamp) / double(clockRate);
  54. }
  55. double RtpPacketizationConfig::timestampToSeconds(uint32_t timestamp) {
  56. return RtpPacketizationConfig::getSecondsFromTimestamp(timestamp, clockRate);
  57. }
  58. uint32_t RtpPacketizationConfig::getTimestampFromSeconds(double seconds, uint32_t clockRate) {
  59. return uint32_t(int64_t(seconds * double(clockRate))); // convert to integer then cast to u32
  60. }
  61. uint32_t RtpPacketizationConfig::secondsToTimestamp(double seconds) {
  62. return RtpPacketizationConfig::getTimestampFromSeconds(seconds, clockRate);
  63. }
  64. } // namespace rtc
  65. #endif /* RTC_ENABLE_MEDIA */