Factor out some more useful functions
[experiments/fps-limit.git] / fps-limit.c
1 /*
2  * fps-limit - Example program to limit frames per seconds
3  *
4  * Copyright (C) 2013  Antonio Ospite <ospite@studenti.unina.it>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <time.h>
23
24 #ifdef DEBUG
25 #define dbg(...)                     \
26         do {                         \
27                 printf(__VA_ARGS__); \
28                 printf("\n");        \
29                 fflush(stdout);      \
30         } while(0)
31 #else
32 #define dbg(...) do {} while(0)
33 #endif
34
35 #define FPS 60
36
37 #define NSEC_PER_SEC 1000000000
38
39 #define timespecsub(a, b, result)                                \
40         do {                                                     \
41                 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec;    \
42                 (result)->tv_nsec = (a)->tv_nsec - (b)->tv_nsec; \
43                 if ((result)->tv_nsec < 0) {                     \
44                         --(result)->tv_sec;                      \
45                         (result)->tv_nsec += 1000000000;         \
46                 }                                                \
47         } while(0)
48
49 struct fps_limit_stats {
50         struct timespec time_start;
51         struct timespec time_end;
52         unsigned int fps;
53 };
54
55 static void fps_limit_init(struct fps_limit_stats *stats, unsigned int fps)
56 {
57         memset(stats, 0, sizeof(*stats));
58
59         clock_gettime(CLOCK_MONOTONIC, &stats->time_start);
60         dbg("Init time: s: %ld, ns: %ld", stats->time_start.tv_sec, stats->time_start.tv_nsec);
61
62         stats->fps = fps;
63 }
64
65 static void fps_limit_sleep(struct fps_limit_stats *stats)
66 {
67         struct timespec elapsed;
68
69         dbg("Start time: s: %ld, ns: %ld", stats->time_start.tv_sec, stats->time_start.tv_nsec);
70
71         clock_gettime(CLOCK_MONOTONIC, &stats->time_end);
72         dbg("End time: s: %ld, ns: %ld", stats->time_end.tv_sec, stats->time_end.tv_nsec);
73
74         timespecsub(&stats->time_end, &stats->time_start, &elapsed);
75         dbg("Elapsed s: %ld ns: %ld", elapsed.tv_sec, elapsed.tv_nsec);
76
77         if (elapsed.tv_sec == 0 && elapsed.tv_nsec < NSEC_PER_SEC / stats->fps) {
78                 struct timespec remaining = {0, 0};
79
80                 /* remaining delay, _relative_ to time_end */
81                 remaining.tv_nsec = (NSEC_PER_SEC / stats->fps) - elapsed.tv_nsec;
82                 dbg("Sleeping for: ns: %ld", remaining.tv_nsec);
83
84                 clock_nanosleep(CLOCK_MONOTONIC, 0, &remaining, NULL);
85         }
86
87         /* update the stats for the next iteration */
88         clock_gettime(CLOCK_MONOTONIC, &stats->time_start);
89 }
90
91 int main(void)
92 {
93         struct fps_limit_stats stats;
94         struct timespec tmp;
95
96         fps_limit_init(&stats, FPS);
97         while (1) {
98                 dbg("Doing some work, max: %d", NSEC_PER_SEC / FPS);
99                 tmp.tv_sec = 0;
100                 tmp.tv_nsec = (NSEC_PER_SEC / FPS) * 2 / 3;
101                 nanosleep(&tmp, NULL);
102
103                 fps_limit_sleep(&stats);
104         }
105
106         return 0;
107 }