This commit is contained in:
JerryXiao 2023-04-26 17:07:19 +08:00
parent 51f0a2e421
commit 0f326d1c44
Signed by: Jerry
GPG key ID: 22618F758B5BE2E5

View file

@ -15,8 +15,7 @@
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
/* 10 frames per second (if we can) */ #define FPS 10
#define PICTURE_TIMEOUT (1.0/10.0)
#define UINPUT_ABS_MAX INT16_MAX #define UINPUT_ABS_MAX INT16_MAX
#define UINPUT_MAX_KEY 256 #define UINPUT_MAX_KEY 256
@ -26,22 +25,27 @@ struct Vec2d {
}; };
struct Vec2d resolution; struct Vec2d resolution;
/* #define SLEEPNS (1000000000 / FPS)
* throttle camera updates static void between_frames() {
*/ static struct timespec now={0,0}, then={0,0}, tmp={0,0};
static int TimeToTakePicture() {
static struct timeval now={0,0}, then={0,0};
double elapsed, dnow, dthen;
gettimeofday(&now,NULL); clock_gettime(CLOCK_MONOTONIC, &now);
memcpy((char*)&then, (char*)&tmp, sizeof(struct timespec));
dnow = now.tv_sec + (now.tv_usec /1000000.0); tmp.tv_nsec += SLEEPNS;
dthen = then.tv_sec + (then.tv_usec/1000000.0); if (tmp.tv_nsec >= 1000000000) {
elapsed = dnow - dthen; tmp.tv_sec ++;
tmp.tv_nsec %= 1000000000;
if (elapsed > PICTURE_TIMEOUT) }
memcpy((char *)&then, (char *)&now, sizeof(struct timeval)); if (now.tv_sec < tmp.tv_sec || (now.tv_sec == tmp.tv_sec && now.tv_nsec < tmp.tv_nsec)) {
return elapsed > PICTURE_TIMEOUT; then.tv_sec = tmp.tv_sec - now.tv_sec;
then.tv_nsec = tmp.tv_nsec - now.tv_nsec;
if (then.tv_nsec < 0) {
then.tv_sec --;
then.tv_nsec += 1000000000;
}
nanosleep(&then, &then);
}
memcpy((char*)&now, (char*)&then, sizeof(struct timespec));
} }
static void convert_bgrx_to_rgb(const char* in, int width, int height, char* buff) { static void convert_bgrx_to_rgb(const char* in, int width, int height, char* buff) {
@ -477,9 +481,7 @@ int main(int argc, const char **argv)
size_t buflen = fb->width * fb->height * 32 / 8; size_t buflen = fb->width * fb->height * 32 / 8;
char *buf = malloc(buflen); char *buf = malloc(buflen);
char *buf2 = malloc(buflen);
memset(buf, 0, buflen); memset(buf, 0, buflen);
memset(buf2, 0, buflen);
resolution.x = fb->width; resolution.x = fb->width;
resolution.y = fb->height; resolution.y = fb->height;
@ -498,11 +500,11 @@ int main(int argc, const char **argv)
rfbInitServer(server); rfbInitServer(server);
rfbRunEventLoop(server,-1,TRUE); rfbRunEventLoop(server,-1,TRUE);
while (rfbIsActive(server)) { while (rfbIsActive(server)) {
if (server->clientHead && TimeToTakePicture()) { between_frames();
if (server->clientHead) {
funcs.sync_start(primefd); funcs.sync_start(primefd);
memcpy(buf2, mapped, buflen); funcs.convert(mapped, fb->width, fb->height, buf);
funcs.sync_end(primefd); funcs.sync_end(primefd);
funcs.convert(buf2, fb->width, fb->height, buf);
rfbMarkRectAsModified(server, 0, 0, fb->width, fb->height); rfbMarkRectAsModified(server, 0, 0, fb->width, fb->height);
} }
} }