-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
136 lines (113 loc) · 3.38 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <stdio.h>
#include <stdbool.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/ioctl.h>
#include <linux/dma-buf.h>
#include "parameters.h"
#include "pipe.h"
#include "camera.h"
#include "text.h"
#include "encoder.h"
static int pipe_video_fd;
static pthread_mutex_t pipe_video_mutex;
static text_t *text;
static encoder_t *enc;
static void on_frame(
uint8_t *mapped,
int fd,
uint64_t size,
uint64_t ts) {
// mapped DMA buffers require a DMA_BUF_IOCTL_SYNC before and after usage.
// https://forums.raspberrypi.com/viewtopic.php?t=352554
struct dma_buf_sync dma_sync = {0};
dma_sync.flags = DMA_BUF_SYNC_START | DMA_BUF_SYNC_RW;
ioctl(fd, DMA_BUF_IOCTL_SYNC, &dma_sync);
text_draw(text, mapped);
dma_sync.flags = DMA_BUF_SYNC_END | DMA_BUF_SYNC_RW;
ioctl(fd, DMA_BUF_IOCTL_SYNC, &dma_sync);
encoder_encode(enc, mapped, fd, size, ts);
}
static void on_encoder_output(const uint8_t *mapped, uint64_t size, uint64_t ts) {
pthread_mutex_lock(&pipe_video_mutex);
pipe_write_buf(pipe_video_fd, mapped, size, ts);
pthread_mutex_unlock(&pipe_video_mutex);
}
int main() {
if (getenv("TEST") != NULL) {
printf("test passed\n");
return 0;
}
int pipe_conf_fd = atoi(getenv("PIPE_CONF_FD"));
pipe_video_fd = atoi(getenv("PIPE_VIDEO_FD"));
uint8_t *buf;
uint32_t n = pipe_read(pipe_conf_fd, &buf);
parameters_t params;
bool ok = parameters_unserialize(¶ms, &buf[1], n-1);
free(buf);
if (!ok) {
pipe_write_error(pipe_video_fd, "parameters_unserialize(): %s", parameters_get_error());
return -1;
}
pthread_mutex_init(&pipe_video_mutex, NULL);
pthread_mutex_lock(&pipe_video_mutex);
camera_t *cam;
ok = camera_create(
¶ms,
on_frame,
&cam);
if (!ok) {
pipe_write_error(pipe_video_fd, "camera_create(): %s", camera_get_error());
return -1;
}
ok = text_create(
¶ms,
camera_get_stride(cam),
&text);
if (!ok) {
pipe_write_error(pipe_video_fd, "text_create(): %s", text_get_error());
return -1;
}
ok = encoder_create(
¶ms,
camera_get_stride(cam),
camera_get_colorspace(cam),
on_encoder_output,
&enc);
if (!ok) {
pipe_write_error(pipe_video_fd, "encoder_create(): %s", encoder_get_error());
return -1;
}
ok = camera_start(cam);
if (!ok) {
pipe_write_error(pipe_video_fd, "camera_start(): %s", camera_get_error());
return -1;
}
pipe_write_ready(pipe_video_fd);
pthread_mutex_unlock(&pipe_video_mutex);
while (true) {
uint8_t *buf;
uint32_t n = pipe_read(pipe_conf_fd, &buf);
switch (buf[0]) {
case 'e':
return 0;
case 'c':
{
parameters_t params;
bool ok = parameters_unserialize(¶ms, &buf[1], n-1);
free(buf);
if (!ok) {
printf("skipping reloading parameters since they are invalid: %s\n", parameters_get_error());
continue;
}
camera_reload_params(cam, ¶ms);
encoder_reload_params(enc, ¶ms);
parameters_destroy(¶ms);
}
}
}
return 0;
}