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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
/*
* This file is part of the OpenKinect Project. http://www.openkinect.org
*
* Copyright (c) 2010 Brandyn White (bwhite@dappervision.com)
* Copyright (c) 2016 Luke Shumaker (lukeshu@sbcglobal.net)
*
* This code is licensed to you under the terms of the Apache License, version
* 2.0, or, at your option, the terms of the GNU General Public License,
* version 2.0. See the APACHE20 and GPL2 files for the text of the licenses,
* or the following URLs:
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.gnu.org/licenses/gpl-2.0.txt
*
* If you redistribute this file in source form, modified or unmodified, you
* may:
* 1) Leave this header intact and distribute it under the same terms,
* accompanying it with the APACHE20 and GPL20 files, or
* 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or
* 3) Delete the GPL v2 clause and accompany it with the APACHE20 file
* In all cases you must keep the copyright notice intact and include a copy
* of the CONTRIB file.
*
* Binary distributions must follow the binary distribution requirements of
* either License.
*/
#include <stdio.h>
#include <errno.h>
#include <error.h>
#include <pthread.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include "main.h"
#include "wg.h"
volatile sig_atomic_t running = 1;
static struct wg wg;
void * xrealloc(void *ptr, size_t size)
{
void *ret = realloc(ptr, size);
if (ret == NULL) {
if (ptr==NULL)
error(1, errno, "Could not allocate memory");
else
error(1, errno, "Could not re-allocate memory");
}
return ret;
}
static
void start_ffmpeg(int in, int out) {
switch (fork()) {
case -1: /* error */
error(1, errno, "fork");
case 0: /* child */
dup2(in, 0);
dup2(out, 1);
for (int fd = 3; fd < 30; fd++)
close(fd);
execlp("ffmpeg",
"ffmpeg",
"-pix_fmt", "rgb24", "-s", "640x480", "-f", "rawvideo", "-i", "-",
"-f", "mpjpeg", "-",
NULL);
error(1, errno, "execlp: ffmpeg");
default: /* parent */
close(in);
close(out);
}
}
struct thread_kinect_args {
int video_fd;
int depth_fd;
int accel_fd;
};
static
void* start_kinect_inner(void *args_anon) {
pthread_setname_np(pthread_self(), "kinect");
struct thread_kinect_args *args = args_anon;
thread_kinect(args->video_fd, args->depth_fd, args->accel_fd);
wg_sub(&wg, 1);
running = 0;
close(httpsock);
return NULL;
}
static
void start_kinect(int video_fd, int depth_fd, int accel_fd) {
static struct thread_kinect_args args;
args.video_fd = video_fd;
args.depth_fd = depth_fd;
args.accel_fd = accel_fd;
static pthread_t thread;
wg_add(&wg, 1);
pthread_create(&thread, NULL, start_kinect_inner, &args);
}
struct thread_mpjpeg_args {
struct mpjpeg_stream *s;
int fd;
const char *boundary;
};
static
void *start_mpjpeg_reader_inner(void *args_anon) {
pthread_setname_np(pthread_self(), "mpjpeg-reader");
struct thread_mpjpeg_args *args = args_anon;
thread_mpjpeg_reader(args->s, args->fd, args->boundary);
wg_sub(&wg, 1);
return NULL;
}
static
void start_mpjpeg_reader(struct mpjpeg_stream *s, int fd, const char *boundary) {
struct thread_mpjpeg {
struct thread_mpjpeg_args args;
pthread_t thread;
};
struct thread_mpjpeg *thread = xrealloc(NULL, sizeof(struct thread_mpjpeg_args));
thread->args.s = s;
thread->args.fd = fd;
thread->args.boundary = boundary;
wg_add(&wg, 1);
pthread_create(&thread->thread, NULL, start_mpjpeg_reader_inner, &thread->args);
}
struct thread_http_args {
struct wg *wg;
struct mpjpeg_stream *video;
struct mpjpeg_stream *depth;
};
static
void *start_http_inner(void *args_anon) {
pthread_setname_np(pthread_self(), "http-listener");
struct thread_http_args *args = args_anon;
thread_http_listener(args->wg, args->video, args->depth);
wg_sub(&wg, 1);
running = 0;
close(httpsock);
return NULL;
}
static
void start_http(struct wg *awg,
struct mpjpeg_stream *video,
struct mpjpeg_stream *depth) {
static struct thread_http_args args;
args.wg = awg;
args.video = video;
args.depth = depth;
static pthread_t thread;
wg_add(&wg, 1);
pthread_create(&thread, NULL, start_http_inner, &args);
}
static
void sigchld_handler(int sig UNUSED)
{
int status;
while (waitpid(-1, &status, WNOHANG) > 0) {}
}
static
void sigint_handler(int num UNUSED)
{
running = 0;
close(httpsock);
}
int main(int argc UNUSED, char **argv UNUSED)
{
int kinect_video_fds[2];
int kinect_depth_fds[2];
int kinect_accel_fds[2];
int mpjpeg_video_fds[2];
int mpjpeg_depth_fds[2];
struct mpjpeg_stream mpjpeg_video;
struct mpjpeg_stream mpjpeg_depth;
struct sigaction act_chld;
sigemptyset (&act_chld.sa_mask);
act_chld.sa_flags = SA_RESTART;
act_chld.sa_handler = sigchld_handler;
if (sigaction(SIGCHLD, &act_chld, 0)) {
error(EXIT_FAILURE, errno, _("Could not set up SIGCHLD handler"));
}
struct sigaction act_int;
sigemptyset (&act_int.sa_mask);
act_int.sa_flags = 0;
act_int.sa_handler = sigint_handler;
if (sigaction(SIGINT, &act_int, 0)) {
error(EXIT_FAILURE, errno, _("Could not set up SIGINT handler"));
}
if (pipe(kinect_video_fds) == -1) error(1, errno, "pipe");
if (pipe(kinect_depth_fds) == -1) error(1, errno, "pipe");
if (pipe(kinect_accel_fds) == -1) error(1, errno, "pipe");
if (pipe(mpjpeg_video_fds) == -1) error(1, errno, "pipe");
if (pipe(mpjpeg_depth_fds) == -1) error(1, errno, "pipe");
init_mpjpeg_stream(&mpjpeg_video);
init_mpjpeg_stream(&mpjpeg_depth);
wg_init(&wg);
start_kinect(kinect_video_fds[1],
kinect_depth_fds[1],
kinect_accel_fds[1]);
start_mpjpeg_reader(&mpjpeg_video, mpjpeg_video_fds[0], "--ffserver");
start_mpjpeg_reader(&mpjpeg_depth, mpjpeg_depth_fds[0], "--ffserver");
start_http(&wg, &mpjpeg_video, &mpjpeg_depth);
/* never call exit() after we've started ffmpeg */
start_ffmpeg(kinect_video_fds[0], mpjpeg_video_fds[1]);
start_ffmpeg(kinect_depth_fds[0], mpjpeg_depth_fds[1]);
wg_wait(&wg);
return 0;
}
|