libcyberradio 22.01.24
Throttle.cpp
1#include <LibCyberRadio/Common/Throttle.hpp>
2
3Throttle::Throttle(double sample_rate)
4{
5 this->sample_rate = sample_rate;
6 this->target_rate = (double)(sample_rate) * (4136/1024); // Convert from samples to bytes
7 this->target_rate /= (1024*1024); // Convert to MB
8 std::chrono::system_clock clock;
9 this->MB_sent = 0;
10 this->start_time = clock.now();
11}
12
13void Throttle::throttle(unsigned int bytes_sent)
14{
15 this->MB_sent += ((double)bytes_sent)/(1048576);
16 std::chrono::time_point<std::chrono::system_clock,std::chrono::nanoseconds> curr_time = this->clock.now();
17 std::chrono::duration<double> elapsed = curr_time - start_time;
18 double current_rate = ((this->MB_sent)) / (elapsed.count());
19
20 if (current_rate > this->target_rate) {
21 usleep(1e4);
22 }
23
24}