-
-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MediaHandler that can be used to pace packet delivery Resolves #1017
- Loading branch information
Showing
5 changed files
with
132 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Copyright (c) 2020 Staz Modrzynski | ||
* Copyright (c) 2020 Paul-Louis Ageneau | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
#ifndef RTC_PACING_HANDLER_H | ||
#define RTC_PACING_HANDLER_H | ||
|
||
#if RTC_ENABLE_MEDIA | ||
|
||
#include "mediahandler.hpp" | ||
#include "utils.hpp" | ||
|
||
#include <atomic> | ||
#include <queue> | ||
|
||
namespace rtc { | ||
|
||
// Paced sending of RTP packets. Takes a stream of RTP packets that can an | ||
// uneven bitrate. It then delivers these packets in a smoother manner by | ||
// sending a fixed size of them on an interval | ||
class RTC_CPP_EXPORT PacingHandler : public MediaHandler { | ||
public: | ||
PacingHandler(double mBytesPerSecond, std::chrono::milliseconds sendInterval); | ||
|
||
void outgoing(message_vector &messages, const message_callback &send) override; | ||
|
||
private: | ||
std::atomic<bool> mHaveScheduled = false; | ||
|
||
double mBytesPerSecond; | ||
double mBudget; | ||
|
||
std::chrono::milliseconds mSendInterval; | ||
std::chrono::time_point<std::chrono::high_resolution_clock> mLastRun; | ||
|
||
std::mutex mMutex; | ||
std::queue<message_ptr> mRtpBuffer = {}; | ||
|
||
void schedule(const message_callback &send); | ||
}; | ||
|
||
} // namespace rtc | ||
|
||
#endif // RTC_ENABLE_MEDIA | ||
|
||
#endif // RTC_PACING_HANDLER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* Copyright (c) 2020 Filip Klembara (in2core) | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
#if RTC_ENABLE_MEDIA | ||
|
||
#include <memory> | ||
|
||
#include "pacinghandler.hpp" | ||
|
||
#include "impl/internals.hpp" | ||
#include "impl/threadpool.hpp" | ||
|
||
namespace rtc { | ||
|
||
PacingHandler::PacingHandler(double bytesPerSecond, std::chrono::milliseconds sendInterval) | ||
: mBytesPerSecond(bytesPerSecond), mSendInterval(sendInterval){}; | ||
|
||
void PacingHandler::schedule(const message_callback &send) { | ||
if (!mHaveScheduled.exchange(true)) { | ||
return; | ||
} | ||
|
||
impl::ThreadPool::Instance().schedule(mSendInterval, [weak_this = weak_from_this(), send]() { | ||
if (auto locked = std::dynamic_pointer_cast<PacingHandler>(weak_this.lock())) { | ||
const std::lock_guard<std::mutex> lock(locked->mMutex); | ||
locked->mHaveScheduled.store(false); | ||
|
||
// Update the budget and cap it | ||
auto newBudget = std::chrono::duration<double>( | ||
std::chrono::high_resolution_clock::now() - locked->mLastRun) * | ||
locked->mBytesPerSecond; | ||
auto maxBudget = | ||
std::chrono::duration<double>(locked->mSendInterval) * locked->mBytesPerSecond; | ||
locked->mBudget = std::min(locked->mBudget + newBudget.count(), maxBudget.count()); | ||
|
||
// Send packets while there is budget, allow a single partial packet over budget | ||
while (!locked->mRtpBuffer.empty() && locked->mBudget > 0) { | ||
auto size = int(locked->mRtpBuffer.front()->size()); | ||
send(std::move(locked->mRtpBuffer.front())); | ||
locked->mRtpBuffer.pop(); | ||
locked->mBudget -= size; | ||
} | ||
|
||
locked->mLastRun = std::chrono::high_resolution_clock::now(); | ||
} | ||
}); | ||
} | ||
|
||
void PacingHandler::outgoing(message_vector &messages, const message_callback &send) { | ||
|
||
std::lock_guard<std::mutex> lock(mMutex); | ||
|
||
while (messages.size() > 0) { | ||
mRtpBuffer.push(std::move(messages.front())); | ||
messages.erase(messages.begin()); | ||
} | ||
|
||
schedule(send); | ||
} | ||
|
||
} // namespace rtc | ||
|
||
#endif /* RTC_ENABLE_MEDIA */ |