pacinghandler.hpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. Takes a stream of RTP packets that can an
  17. // uneven bitrate. It then delivers these packets in a smoother manner by
  18. // sending a fixed size of them on an interval
  19. class RTC_CPP_EXPORT PacingHandler : public MediaHandler {
  20. public:
  21. PacingHandler(double bitsPerSecond, std::chrono::milliseconds sendInterval);
  22. void outgoing(message_vector &messages, const message_callback &send) override;
  23. private:
  24. std::atomic<bool> mHaveScheduled = false;
  25. double mBytesPerSecond;
  26. double mBudget;
  27. std::chrono::milliseconds mSendInterval;
  28. std::chrono::time_point<std::chrono::high_resolution_clock> mLastRun;
  29. std::mutex mMutex;
  30. std::queue<message_ptr> mRtpBuffer;
  31. void schedule(const message_callback &send);
  32. };
  33. } // namespace rtc
  34. #endif // RTC_ENABLE_MEDIA
  35. #endif // RTC_PACING_HANDLER_H