Replace sleep()/usleep() with std::chrono calls (#473)

This commit is contained in:
Frank Dana
2020-09-02 02:07:54 -04:00
committed by GitHub
parent f71051e8f1
commit e500cae9f5
6 changed files with 122 additions and 86 deletions

View File

@@ -31,6 +31,9 @@
#include "../../include/Qt/VideoCacheThread.h"
#include <algorithm>
#include <thread> // for std::this_thread::sleep_for
#include <chrono> // for std::chrono::milliseconds
namespace openshot
{
// Constructor
@@ -81,10 +84,14 @@ namespace openshot
// Start the thread
void VideoCacheThread::run()
{
while (!threadShouldExit() && is_playing) {
// Types for storing time durations in whole and fractional milliseconds
using ms = std::chrono::milliseconds;
using double_ms = std::chrono::duration<double, ms::period>;
// Calculate sleep time for frame rate
double frame_time = (1000.0 / reader->info.fps.ToDouble());
// Calculate on-screen time for a single frame in milliseconds
const auto frame_duration = double_ms(1000.0 / reader->info.fps.ToDouble());
while (!threadShouldExit() && is_playing) {
// Cache frames before the other threads need them
// Cache frames up to the max frames
@@ -117,7 +124,7 @@ namespace openshot
}
// Sleep for 1 frame length
usleep(frame_time * 1000);
std::this_thread::sleep_for(frame_duration);
}
return;