Calculate the limit delay once and for all master
authorAntonio Ospite <ospite@studenti.unina.it>
Tue, 17 Dec 2013 23:15:44 +0000 (00:15 +0100)
committerAntonio Ospite <ospite@studenti.unina.it>
Tue, 17 Dec 2013 23:15:44 +0000 (00:15 +0100)
fps-limit.h

index 376fb77..d19df1c 100644 (file)
@@ -50,7 +50,7 @@
 struct fps_limit_stats {
        struct timespec time_start;
        struct timespec time_end;
 struct fps_limit_stats {
        struct timespec time_start;
        struct timespec time_end;
-       unsigned int fps;
+       unsigned int delay;
 };
 
 static void fps_limit_init(struct fps_limit_stats *stats, unsigned int fps)
 };
 
 static void fps_limit_init(struct fps_limit_stats *stats, unsigned int fps)
@@ -60,7 +60,7 @@ static void fps_limit_init(struct fps_limit_stats *stats, unsigned int fps)
        clock_gettime(CLOCK_MONOTONIC, &stats->time_start);
        fps_limit_dbg("Init time: s: %ld, ns: %ld", stats->time_start.tv_sec, stats->time_start.tv_nsec);
 
        clock_gettime(CLOCK_MONOTONIC, &stats->time_start);
        fps_limit_dbg("Init time: s: %ld, ns: %ld", stats->time_start.tv_sec, stats->time_start.tv_nsec);
 
-       stats->fps = fps;
+       stats->delay = NSEC_PER_SEC / fps;
 }
 
 static void fps_limit_sleep(struct fps_limit_stats *stats)
 }
 
 static void fps_limit_sleep(struct fps_limit_stats *stats)
@@ -75,11 +75,11 @@ static void fps_limit_sleep(struct fps_limit_stats *stats)
        fps_limit_timespecsub(&stats->time_end, &stats->time_start, &elapsed);
        fps_limit_dbg("Elapsed s: %ld ns: %ld", elapsed.tv_sec, elapsed.tv_nsec);
 
        fps_limit_timespecsub(&stats->time_end, &stats->time_start, &elapsed);
        fps_limit_dbg("Elapsed s: %ld ns: %ld", elapsed.tv_sec, elapsed.tv_nsec);
 
-       if (elapsed.tv_sec == 0 && elapsed.tv_nsec < NSEC_PER_SEC / stats->fps) {
+       if (elapsed.tv_sec == 0 && elapsed.tv_nsec < stats->delay) {
                struct timespec remaining = {0, 0};
 
                /* remaining delay, _relative_ to time_end */
                struct timespec remaining = {0, 0};
 
                /* remaining delay, _relative_ to time_end */
-               remaining.tv_nsec = (NSEC_PER_SEC / stats->fps) - elapsed.tv_nsec;
+               remaining.tv_nsec = stats->delay - elapsed.tv_nsec;
                fps_limit_dbg("Sleeping for: ns: %ld", remaining.tv_nsec);
 
                clock_nanosleep(CLOCK_MONOTONIC, 0, &remaining, NULL);
                fps_limit_dbg("Sleeping for: ns: %ld", remaining.tv_nsec);
 
                clock_nanosleep(CLOCK_MONOTONIC, 0, &remaining, NULL);