pacinghandler.hpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /**
  2. * Copyright (c) 2024 Sean DuBois <[email protected]>
  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_PACING_HANDLER_H
  9. #define RTC_PACING_HANDLER_H
  10. #if RTC_ENABLE_MEDIA
  11. #include "mediahandler.hpp"
  12. #include "utils.hpp"
  13. #include <atomic>
  14. #include <queue>
  15. namespace rtc {
  16. // Paced sending of RTP packets. It takes a stream of RTP packets that can have an uneven bitrate
  17. // and delivers them in a smoother manner by sending a fixed size of them on an interval
  18. class RTC_CPP_EXPORT PacingHandler : public MediaHandler {
  19. public:
  20. PacingHandler(double bitsPerSecond, std::chrono::milliseconds sendInterval);
  21. void outgoing(message_vector &messages, const message_callback &send) override;
  22. private:
  23. std::atomic<bool> mHaveScheduled = false;
  24. double mBytesPerSecond;
  25. double mBudget;
  26. std::chrono::milliseconds mSendInterval;
  27. std::chrono::time_point<std::chrono::high_resolution_clock> mLastRun;
  28. std::mutex mMutex;
  29. std::queue<message_ptr> mRtpBuffer;
  30. void schedule(const message_callback &send);
  31. };
  32. } // namespace rtc
  33. #endif // RTC_ENABLE_MEDIA
  34. #endif // RTC_PACING_HANDLER_H