rtcpnackresponder.hpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #ifndef RTC_RTCP_NACK_RESPONDER_H
  9. #define RTC_RTCP_NACK_RESPONDER_H
  10. #if RTC_ENABLE_MEDIA
  11. #include "mediahandlerelement.hpp"
  12. #include <queue>
  13. #include <unordered_map>
  14. namespace rtc {
  15. class RTC_CPP_EXPORT RtcpNackResponder final : public MediaHandlerElement {
  16. /// Packet storage
  17. class RTC_CPP_EXPORT Storage {
  18. /// Packet storage element
  19. struct RTC_CPP_EXPORT Element {
  20. Element(binary_ptr packet, uint16_t sequenceNumber, shared_ptr<Element> next = nullptr);
  21. const binary_ptr packet;
  22. const uint16_t sequenceNumber;
  23. /// Pointer to newer element
  24. shared_ptr<Element> next = nullptr;
  25. };
  26. private:
  27. /// Oldest packet in storage
  28. shared_ptr<Element> oldest = nullptr;
  29. /// Newest packet in storage
  30. shared_ptr<Element> newest = nullptr;
  31. /// Inner storage
  32. std::unordered_map<uint16_t, shared_ptr<Element>> storage{};
  33. /// Maximum storage size
  34. const unsigned maximumSize;
  35. /// Returnst current size
  36. unsigned size();
  37. public:
  38. static const unsigned defaultMaximumSize = 512;
  39. Storage(unsigned _maximumSize);
  40. /// Returns packet with given sequence number
  41. optional<binary_ptr> get(uint16_t sequenceNumber);
  42. /// Stores packet
  43. /// @param packet Packet
  44. void store(binary_ptr packet);
  45. };
  46. const shared_ptr<Storage> storage;
  47. std::mutex reportMutex;
  48. public:
  49. RtcpNackResponder(unsigned maxStoredPacketCount = Storage::defaultMaximumSize);
  50. /// Checks for RTCP NACK and handles it,
  51. /// @param message RTCP message
  52. /// @returns unchanged RTCP message and requested RTP packets
  53. ChainedIncomingControlProduct processIncomingControlMessage(message_ptr message) override;
  54. /// Stores RTP packets in internal storage
  55. /// @param messages RTP packets
  56. /// @param control RTCP
  57. /// @returns Unchanged RTP and RTCP
  58. ChainedOutgoingProduct processOutgoingBinaryMessage(ChainedMessagesProduct messages,
  59. message_ptr control) override;
  60. };
  61. } // namespace rtc
  62. #endif /* RTC_ENABLE_MEDIA */
  63. #endif /* RTC_RTCP_NACK_RESPONDER_H */