2
0

rtcpnackresponder.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 "rtcpnackresponder.hpp"
  20. #include "impl/internals.hpp"
  21. #include <cassert>
  22. namespace rtc {
  23. RtcpNackResponder::Storage::Element::Element(binary_ptr packet, uint16_t sequenceNumber,
  24. shared_ptr<Element> next)
  25. : packet(packet), sequenceNumber(sequenceNumber), next(next) {}
  26. unsigned RtcpNackResponder::Storage::size() { return storage.size(); }
  27. RtcpNackResponder::Storage::Storage(unsigned _maximumSize) : maximumSize(_maximumSize) {
  28. assert(maximumSize > 0);
  29. storage.reserve(maximumSize);
  30. }
  31. optional<binary_ptr> RtcpNackResponder::Storage::get(uint16_t sequenceNumber) {
  32. auto position = storage.find(sequenceNumber);
  33. return position != storage.end() ? std::make_optional(storage.at(sequenceNumber)->packet)
  34. : nullopt;
  35. }
  36. void RtcpNackResponder::Storage::store(binary_ptr packet) {
  37. if (!packet || packet->size() < 12) {
  38. return;
  39. }
  40. auto rtp = reinterpret_cast<RTP *>(packet->data());
  41. auto sequenceNumber = rtp->seqNumber();
  42. assert((storage.empty() && !oldest && !newest) || (!storage.empty() && oldest && newest));
  43. if (size() == 0) {
  44. newest = std::make_shared<Element>(packet, sequenceNumber);
  45. oldest = newest;
  46. } else {
  47. auto current = std::make_shared<Element>(packet, sequenceNumber);
  48. newest->next = current;
  49. newest = current;
  50. }
  51. storage.emplace(sequenceNumber, newest);
  52. if (size() > maximumSize) {
  53. assert(oldest);
  54. if (oldest) {
  55. storage.erase(oldest->sequenceNumber);
  56. oldest = oldest->next;
  57. }
  58. }
  59. }
  60. RtcpNackResponder::RtcpNackResponder(unsigned maxStoredPacketCount)
  61. : MediaHandlerElement(), storage(std::make_shared<Storage>(maxStoredPacketCount)) {}
  62. ChainedIncomingControlProduct
  63. RtcpNackResponder::processIncomingControlMessage(message_ptr message) {
  64. optional<ChainedOutgoingProduct> optPackets = ChainedOutgoingProduct(nullptr);
  65. auto packets = make_chained_messages_product();
  66. unsigned int i = 0;
  67. while (i < message->size()) {
  68. auto nack = reinterpret_cast<RTCP_NACK *>(message->data() + i);
  69. i += nack->header.header.lengthInBytes();
  70. // check if rtcp is nack
  71. if (nack->header.header.payloadType() != 205 || nack->header.header.reportCount() != 1) {
  72. continue;
  73. }
  74. auto fieldsCount = nack->getSeqNoCount();
  75. std::vector<uint16_t> missingSequenceNumbers{};
  76. for (unsigned int i = 0; i < fieldsCount; i++) {
  77. auto field = nack->parts[i];
  78. auto newMissingSeqenceNumbers = field.getSequenceNumbers();
  79. missingSequenceNumbers.insert(missingSequenceNumbers.end(),
  80. newMissingSeqenceNumbers.begin(),
  81. newMissingSeqenceNumbers.end());
  82. }
  83. packets->reserve(packets->size() + missingSequenceNumbers.size());
  84. for (auto sequenceNumber : missingSequenceNumbers) {
  85. auto optPacket = storage->get(sequenceNumber);
  86. if (optPacket.has_value()) {
  87. auto packet = optPacket.value();
  88. packets->push_back(packet);
  89. }
  90. }
  91. }
  92. if (!packets->empty()) {
  93. return {message, ChainedOutgoingProduct(packets)};
  94. } else {
  95. return {message, nullopt};
  96. }
  97. }
  98. ChainedOutgoingProduct
  99. RtcpNackResponder::processOutgoingBinaryMessage(ChainedMessagesProduct messages,
  100. message_ptr control) {
  101. for (auto message : *messages) {
  102. storage->store(message);
  103. }
  104. return {messages, control};
  105. }
  106. } // namespace rtc
  107. #endif /* RTC_ENABLE_MEDIA */