-
-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
147 additions
and
3 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
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,44 @@ | ||
#ifndef PACER_H | ||
#define PACER_H | ||
|
||
#if RTC_ENABLE_MEDIA | ||
|
||
#include <deque> | ||
#include <chrono> | ||
#include "mediahandler.hpp" | ||
|
||
namespace rtc { | ||
|
||
class RTC_CPP_EXPORT PacerAlgorithm { | ||
public: | ||
virtual unsigned int getBudget() = 0; | ||
virtual unsigned int getPace() = 0; | ||
virtual void setPace(unsigned int pace) = 0; | ||
virtual void setBudget(unsigned int budget) = 0; | ||
virtual void resetBudget() = 0; | ||
}; | ||
|
||
class RTC_CPP_EXPORT Metronome final : public MediaHandler { | ||
std::deque<message_ptr> mSendQueue; | ||
size_t mQueueSizeInBytes; | ||
size_t mMaxQueueSizeInBytes; | ||
std::mutex mSendQueueMutex; | ||
|
||
std::chrono::milliseconds mThreadDelay; | ||
std::function<void(message_vector&)> mProcessPacketsCallback; // For bookkeeping of sent packets | ||
std::shared_ptr<PacerAlgorithm> mPacerAlgorithm; | ||
|
||
public: | ||
Metronome(size_t maxQueueSizeInBytes, std::shared_ptr<PacerAlgorithm> pacerAlgorithm, | ||
std::function<void(message_vector &)> processPacketsCallback); | ||
|
||
void outgoing(message_vector &messages, const message_callback &send) override; | ||
void senderProcess(const message_callback &send); | ||
|
||
}; | ||
|
||
} // namespace rtc | ||
|
||
#endif /* RTC_ENABLE_MEDIA */ | ||
|
||
#endif /* PACER_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
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 @@ | ||
#if RTC_ENABLE_MEDIA | ||
|
||
#include "metronome.hpp" | ||
#include "impl/threadpool.hpp" | ||
#include "rtp.hpp" | ||
|
||
namespace rtc { | ||
|
||
using ThreadPool = rtc::impl::ThreadPool; | ||
|
||
Metronome::Metronome(size_t maxQueueSizeInBytes, std::shared_ptr<PacerAlgorithm> pacer, | ||
std::function<void(message_vector &)> processPacketsCallback) | ||
: mMaxQueueSizeInBytes(maxQueueSizeInBytes), mPacerAlgorithm(pacer), | ||
mProcessPacketsCallback(processPacketsCallback), mThreadDelay(5), mQueueSizeInBytes(0) {} | ||
|
||
void Metronome::outgoing(message_vector &messages, const message_callback &send) { | ||
message_vector others; | ||
std::unique_lock<std::mutex> lock(mSendQueueMutex); | ||
for (auto &message : messages) { | ||
if (message->type != Message::Binary) { | ||
others.push_back(std::move(message)); | ||
continue; | ||
} | ||
mQueueSizeInBytes += message->size(); | ||
mSendQueue.push_back(std::move(message)); | ||
} | ||
while (mQueueSizeInBytes > mMaxQueueSizeInBytes) { | ||
size_t msg_size = mSendQueue.front()->size(); | ||
// I am not dropping a packet that is just over the limit. | ||
if (mMaxQueueSizeInBytes > mQueueSizeInBytes - msg_size) | ||
break; | ||
mQueueSizeInBytes -= msg_size; | ||
mSendQueue.pop_front(); | ||
} | ||
messages.swap(others); | ||
ThreadPool::Instance().schedule(std::chrono::milliseconds(0), [this, &send]() { senderProcess(send); }); | ||
} | ||
|
||
void Metronome::senderProcess(const message_callback &send) { | ||
if (!mSendQueue.empty()) { | ||
unsigned int budget = mPacerAlgorithm->getBudget(); | ||
message_vector outgoing; | ||
{ | ||
std::unique_lock<std::mutex> lock(mSendQueueMutex); | ||
while (!mSendQueue.empty() && budget >= mSendQueue.front()->size()) { | ||
size_t msg_size = mSendQueue.front()->size(); | ||
budget -= msg_size; | ||
mQueueSizeInBytes -= msg_size; | ||
outgoing.push_back(std::move(mSendQueue.front())); | ||
mSendQueue.pop_front(); | ||
} | ||
} | ||
mPacerAlgorithm->setBudget(budget); | ||
for (const auto &message : outgoing) { | ||
send(message); | ||
} | ||
if (mProcessPacketsCallback) { | ||
mProcessPacketsCallback(outgoing); | ||
} | ||
if (!mSendQueue.empty()) { | ||
ThreadPool::Instance().schedule(mThreadDelay, [this, &send]() { senderProcess(send); }); | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
#endif |