-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
153 lines (126 loc) · 3.77 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#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_out_fd;
static pthread_mutex_t pipe_out_mutex;
static camera_t *cam;
static text_t *text;
static encoder_t *enc;
static void on_frame(
uint8_t *buffer_mapped,
int buffer_fd,
uint64_t buffer_size,
uint64_t timestamp) {
// 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(buffer_fd, DMA_BUF_IOCTL_SYNC, &dma_sync);
text_draw(text, buffer_mapped);
dma_sync.flags = DMA_BUF_SYNC_END | DMA_BUF_SYNC_RW;
ioctl(buffer_fd, DMA_BUF_IOCTL_SYNC, &dma_sync);
encoder_encode(enc, buffer_mapped, buffer_fd, buffer_size, timestamp);
}
static void on_encoder_output(const uint8_t *buffer_mapped, uint64_t buffer_size, uint64_t timestamp) {
pthread_mutex_lock(&pipe_out_mutex);
pipe_write_buf(pipe_out_fd, buffer_mapped, buffer_size, timestamp);
pthread_mutex_unlock(&pipe_out_mutex);
}
static void on_error() {
pthread_mutex_lock(&pipe_out_mutex);
pipe_write_error(pipe_out_fd, "camera driver exited");
pthread_mutex_unlock(&pipe_out_mutex);
}
static bool handle_command(const uint8_t *buf, uint32_t size) {
switch (buf[0]) {
case 'e':
return false;
case 'c':
{
parameters_t params;
bool ok = parameters_unserialize(¶ms, &buf[1], size-1);
if (!ok) {
printf("skipping reloading parameters since they are invalid: %s\n", parameters_get_error());
return true;
}
camera_reload_params(cam, ¶ms);
encoder_reload_params(enc, ¶ms);
parameters_destroy(¶ms);
}
}
return true;
}
int main() {
if (getenv("TEST") != NULL) {
printf("test passed\n");
return 0;
}
int pipe_in_fd = atoi(getenv("PIPE_CONF_FD"));
pipe_out_fd = atoi(getenv("PIPE_VIDEO_FD"));
uint8_t *buf;
uint32_t n = pipe_read(pipe_in_fd, &buf);
parameters_t params;
bool ok = parameters_unserialize(¶ms, &buf[1], n-1);
free(buf);
if (!ok) {
pipe_write_error(pipe_out_fd, "parameters_unserialize(): %s", parameters_get_error());
return -1;
}
pthread_mutex_init(&pipe_out_mutex, NULL);
pthread_mutex_lock(&pipe_out_mutex);
ok = camera_create(
¶ms,
on_frame,
on_error,
&cam);
if (!ok) {
pipe_write_error(pipe_out_fd, "camera_create(): %s", camera_get_error());
return -1;
}
ok = text_create(
¶ms,
camera_get_stride(cam),
&text);
if (!ok) {
pipe_write_error(pipe_out_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_out_fd, "encoder_create(): %s", encoder_get_error());
return -1;
}
ok = camera_start(cam);
if (!ok) {
pipe_write_error(pipe_out_fd, "camera_start(): %s", camera_get_error());
return -1;
}
pipe_write_ready(pipe_out_fd);
pthread_mutex_unlock(&pipe_out_mutex);
while (true) {
uint8_t *buf;
uint32_t size = pipe_read(pipe_in_fd, &buf);
bool ok = handle_command(buf, size);
free(buf);
if (!ok) {
break;
}
}
return 0;
}