Skip to content

Commit 61e7922

Browse files
authored
support displaying microseconds in text overlay (bluenviron/mediamtx#3866) (#47)
Fixes bluenviron/mediamtx#3866
1 parent 9fd8469 commit 61e7922

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

encoder.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <fcntl.h>
66
#include <sys/ioctl.h>
77
#include <unistd.h>
8-
#include <time.h>
98

109
#include <linux/videodev2.h>
1110

text.c

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <sys/time.h>
12
#include <time.h>
23

34
#include <ft2build.h>
@@ -19,6 +20,43 @@ const char *text_get_error() {
1920
return errbuf;
2021
}
2122

23+
static void str_replace(char *buffer, size_t bufsize, const char *find, const char *replace) {
24+
char *ptr = strstr(buffer, find);
25+
if (ptr != NULL) {
26+
int64_t find_len = strlen(find);
27+
int64_t replace_len = strlen(replace);
28+
int64_t after_len = strlen(ptr + find_len);
29+
30+
int64_t avail1 = bufsize - (ptr - buffer) - 1;
31+
if (replace_len > avail1) {
32+
replace_len = avail1;
33+
}
34+
35+
int64_t avail2 = avail1 - replace_len;
36+
if (avail2 <= 0) {
37+
after_len = 0;
38+
} else if (after_len > avail2) {
39+
after_len = avail2;
40+
}
41+
42+
char tmp[256];
43+
memcpy(tmp, ptr + find_len, after_len);
44+
memcpy(ptr, replace, replace_len);
45+
memcpy(ptr + replace_len, tmp, after_len);
46+
ptr[replace_len + after_len] = 0x00;
47+
}
48+
}
49+
50+
static void extended_strftime(char *buffer, size_t bufsize, const char *format, const struct timeval *time) {
51+
struct tm *tm_info = localtime(&time->tv_sec);
52+
strftime(buffer, bufsize, format, tm_info);
53+
54+
char str_usec[7];
55+
sprintf(str_usec, "%.6ld", time->tv_usec);
56+
57+
str_replace(buffer, bufsize, "%f", str_usec);
58+
}
59+
2260
typedef struct {
2361
bool enabled;
2462
char *text_overlay;
@@ -138,11 +176,11 @@ void text_draw(text_t *text, uint8_t *buf) {
138176
text_priv_t *textp = (text_priv_t *)text;
139177

140178
if (textp->enabled) {
141-
time_t timer = time(NULL);
142-
struct tm *tm_info = localtime(&timer);
143-
char buffer[255];
144-
memset(buffer, 0, sizeof(buffer));
145-
strftime(buffer, 255, textp->text_overlay, tm_info);
179+
struct timeval now;
180+
gettimeofday(&now, NULL);
181+
182+
char buffer[256];
183+
extended_strftime(buffer, 256, textp->text_overlay, &now);
146184

147185
draw_rect(
148186
buf,

0 commit comments

Comments
 (0)