summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@sbcglobal.net>2016-03-11 15:48:18 -0500
committerLuke Shumaker <lukeshu@sbcglobal.net>2016-03-11 15:48:18 -0500
commit77f1df1a250d958317c930e6f3a36edf42f11d09 (patch)
tree12ced48f47ccc33398edd591ca5e38275d1ac058
parentba34d24d942e186d993732d7a19b5a32544845b3 (diff)
fixes
-rw-r--r--main.c47
-rw-r--r--main.h1
-rw-r--r--thread_http.c17
-rw-r--r--thread_kinect.c21
-rw-r--r--thread_mpjpeg.c33
5 files changed, 64 insertions, 55 deletions
diff --git a/main.c b/main.c
index df892ac..9af7dd7 100644
--- a/main.c
+++ b/main.c
@@ -64,7 +64,7 @@ void start_ffmpeg(int in, int out) {
close(fd);
execlp("ffmpeg",
"ffmpeg",
- "-pix_fmt", "rgb24", "-s", "640x480", "-f", "rawvideo", "-i", "-",
+ "-pix_fmt", "rgb24", "-s", "640x480", "-f", "rawvideo", "-i", "/dev/stdin",
"-f", "mpjpeg", "-",
NULL);
error(1, errno, "execlp: ffmpeg");
@@ -85,8 +85,7 @@ void* start_kinect_inner(void *args_anon) {
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);
+ finish(0);
return NULL;
}
static
@@ -117,16 +116,13 @@ void *start_mpjpeg_reader_inner(void *args_anon) {
}
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;
+ struct thread_mpjpeg_args *args = xrealloc(NULL, sizeof(struct thread_mpjpeg_args));
+ args->s = s;
+ args->fd = fd;
+ args->boundary = boundary;
wg_add(&wg, 1);
- pthread_create(&thread->thread, NULL, start_mpjpeg_reader_inner, &thread->args);
+ static pthread_t thread;
+ pthread_create(&thread, NULL, start_mpjpeg_reader_inner, args);
}
struct thread_http_args {
@@ -140,8 +136,7 @@ void *start_http_inner(void *args_anon) {
struct thread_http_args *args = args_anon;
thread_http_listener(args->wg, args->video, args->depth);
wg_sub(&wg, 1);
- running = 0;
- close(httpsock);
+ finish(0);
return NULL;
}
static
@@ -165,11 +160,13 @@ void sigchld_handler(int sig UNUSED)
while (waitpid(-1, &status, WNOHANG) > 0) {}
}
-static
-void sigint_handler(int num UNUSED)
+void finish(int sig)
{
+ if (sig != 0)
+ printf("Caught signal %d\n", sig);
running = 0;
close(httpsock);
+ kill(getpid(), SIGHUP);
}
int main(int argc UNUSED, char **argv UNUSED)
@@ -190,13 +187,13 @@ int main(int argc UNUSED, char **argv UNUSED)
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"));
- }
+ struct sigaction act_exit;
+ sigemptyset (&act_exit.sa_mask);
+ act_exit.sa_flags = 0;
+ act_exit.sa_handler = finish;
+ sigaction(SIGINT, &act_exit, 0);
+ sigaction(SIGQUIT, &act_exit, 0);
+ sigaction(SIGTERM, &act_exit, 0);
if (pipe(kinect_video_fds) == -1) error(1, errno, "pipe");
if (pipe(kinect_depth_fds) == -1) error(1, errno, "pipe");
@@ -210,8 +207,8 @@ int main(int argc UNUSED, char **argv UNUSED)
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_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]);
diff --git a/main.h b/main.h
index 32495fa..73fa96e 100644
--- a/main.h
+++ b/main.h
@@ -30,6 +30,7 @@ struct mpjpeg_stream {
struct wg;
+void finish(int sig);
void thread_kinect(int video_fd, int depth_fd, int accel_fd);
void thread_http_listen(int fd);
void thread_mpjpeg_reader(struct mpjpeg_stream *s, int fd, const char *boundary);
diff --git a/thread_http.c b/thread_http.c
index b257131..353ad0f 100644
--- a/thread_http.c
+++ b/thread_http.c
@@ -69,21 +69,23 @@ void thread_http_connection(int fd) {
while (strcmp(line_buf, "\r\n") != 0)
getline(&line_buf, &line_cap, stream);
- if (strcmp(path, "/kinect-video.mpjpg") == 0) {
+ const char *boundary = "boundary";
+ if (strcmp(path, "/kinect-video.mjpg") == 0) {
dprintf(fd,
"HTTP/1.1 200 OK\r\n"
"Content-Type: multipart/x-mixed-replace;boundary=%s\r\n"
"\r\n",
- "--boundary");
- thread_mpjpeg_writer(video, fd, "--boundary");
- } else if (strcmp(path, "/kinect-depth.mpjpg") == 0) {
+ boundary);
+ thread_mpjpeg_writer(video, fd, boundary);
+ } else if (strcmp(path, "/kinect-depth.mjpg") == 0) {
dprintf(fd,
"HTTP/1.1 200 OK\r\n"
"Content-Type: multipart/x-mixed-replace;boundary=%s\r\n"
"\r\n",
- "--boundary");
- thread_mpjpeg_writer(depth, fd, "--boundary");
+ boundary);
+ thread_mpjpeg_writer(depth, fd, boundary);
} else {
+ printf("not found url: %s\n", path);
dprintf(fd,
"HTTP/1.1 404 Not Found\r\n"
"\r\n");
@@ -123,6 +125,5 @@ void thread_http_listener(struct wg *awg,
continue;
start_http_connection(conn);
}
- running = 0;
- close(httpsock);
+ finish(0);
}
diff --git a/thread_kinect.c b/thread_kinect.c
index fdd7f25..bd70679 100644
--- a/thread_kinect.c
+++ b/thread_kinect.c
@@ -13,22 +13,21 @@ FILE *accel_stream = NULL;
static
void dump_ffmpeg_24(FILE *stream, uint32_t timestamp UNUSED, void *data,
- int data_size)
+ size_t data_size)
{
fwrite(data, data_size, 1, stream);
}
static
-void dump_ffmpeg_pad16(FILE *stream, uint32_t timestamp UNUSED, void *data,
- int data_size)
+void dump_ffmpeg_pad16(FILE *stream, uint32_t timestamp UNUSED, void *data_anon,
+ size_t data_size)
{
- unsigned int z = 0;
- uint16_t* data_ptr = (uint16_t*)data;
- uint16_t* end = data_ptr + data_size;
- while (data_ptr < end) {
- z = *data_ptr;
+ uint16_t* data = data_anon;
+ uint16_t* end = (void*)&((char*)data_anon)[data_size];
+ while (data < end) {
+ uint32_t z = *data;
fwrite(((char*)(&z)), 3, 1, stream);
- data_ptr += 2;
+ data = &data[1];
}
}
@@ -37,12 +36,13 @@ void handle_accel(freenect_device *dev UNUSED, freenect_raw_tilt_state* data)
{
double x, y, z;
freenect_get_mks_accel(data, &x, &y, &z);
- fprintf(accel_stream, "x=%f\ty=%f\tz=%f\n", x, y, z);
+ //fprintf(accel_stream, "x=%f\ty=%f\tz=%f\n", x, y, z);
}
static
void handle_depth(freenect_device *dev UNUSED, void *depth, uint32_t timestamp)
{
+ printf("handle depth\n");
dump_ffmpeg_pad16(depth_stream, timestamp, depth,
freenect_find_depth_mode(FREENECT_RESOLUTION_MEDIUM,
FREENECT_DEPTH_11BIT).bytes);
@@ -51,6 +51,7 @@ void handle_depth(freenect_device *dev UNUSED, void *depth, uint32_t timestamp)
static
void handle_video(freenect_device *dev, void *rgb, uint32_t timestamp)
{
+ printf("handle depth\n");
dump_ffmpeg_24(video_stream, timestamp, rgb,
freenect_get_current_video_mode(dev).bytes);
}
diff --git a/thread_mpjpeg.c b/thread_mpjpeg.c
index 112e1a2..410faa0 100644
--- a/thread_mpjpeg.c
+++ b/thread_mpjpeg.c
@@ -9,12 +9,15 @@
#include "main.h"
static
-char *add_nl(const char *old) {
+char *format_boundary(const char *old) {
size_t len = strlen(old);
- char *new = xrealloc(NULL, len+2);
- strcpy(new, old);
- new[len+0] = '\n';
- new[len+1] = '\0';
+ char *new = xrealloc(NULL, len+5);
+ new[0] = '-';
+ new[1] = '-';
+ strcpy(&new[2], old);
+ new[2+len+0] = '\r';
+ new[2+len+1] = '\n';
+ new[2+len+2] = '\0';
return new;
}
@@ -22,21 +25,25 @@ char *add_nl(const char *old) {
void thread_mpjpeg_reader(struct mpjpeg_stream *s, int fd, const char *boundary) {
FILE *stream = fdopen(fd, "r");
- boundary = add_nl(boundary);
+ boundary = format_boundary(boundary);
char *line_buf = NULL;
size_t line_cap = 0;
+ ssize_t line_len = 0;
while (1) {
+ printf("getting frame\n");
s->back->len = -1;
/* read the frame header (MIME headers) */
- getline(&line_buf, &line_cap, stream);
+ line_len = getline(&line_buf, &line_cap, stream);
if (strcmp(line_buf, boundary) != 0)
return;
ssize_t framelen = -1;
- while (strcmp(line_buf, "\r\n") != 0) {
- if (STARTS_WITH(line_buf, "Content-Type:")) {
- s->back->len = atoi(&line_buf[sizeof("Content-Type:")-1]);
+ while (strcmp(line_buf, "\r\n") != 0 && line_len >= 0) {
+ line_len = getline(&line_buf, &line_cap, stream);
+ if (STARTS_WITH(line_buf, "Content-Type:") == 0) {
+ printf("line: <%s>\n", line_buf);
+ s->back->len = atoi(&line_buf[strlen("Content-Type:")]);
}
}
if (s->back->len < 0)
@@ -47,7 +54,9 @@ void thread_mpjpeg_reader(struct mpjpeg_stream *s, int fd, const char *boundary)
s->back->data = xrealloc(s->back->data, s->back->cap = s->back->len);
if (fread(line_buf, framelen, 1, stream) != 1)
return;
-
+
+ printf("got frame on fd %d\n", fd);
+
/* swap the frames */
pthread_mutex_lock(&s->frontlock);
struct frame *tmp = s->front;
@@ -74,7 +83,7 @@ void thread_mpjpeg_writer(struct mpjpeg_stream *s, int fd, const char *boundary)
lastframe = s->front;
pthread_mutex_unlock(&s->frontlock);
/* send the frame to the client */
- if (dprintf(fd, "%s\r\nContent-Type: image/jpeg\r\nContent-Length: %zd\r\n\r\n", boundary, myframe.len) < 0)
+ if (dprintf(fd, "--%s\r\nContent-Type: image/jpeg\r\nContent-Length: %zd\r\n\r\n", boundary, myframe.len) < 0)
return;
if (write(fd, myframe.data, myframe.len) < myframe.len)
return;