From e4dce6b0d0a465f792d1de70c0c65c2fcb8d196c Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sat, 12 Mar 2016 02:51:12 -0500 Subject: fixity fix --- freenect-server--http.c | 5 ++++- freenect-server--kinect.c | 6 ++++-- freenect-server.sh | 15 ++++++++------- mpjpeg.c | 42 +++++++++++++++++++++++++++--------------- mpjpeg.h | 10 ++++------ 5 files changed, 47 insertions(+), 31 deletions(-) diff --git a/freenect-server--http.c b/freenect-server--http.c index 6871ffb..cda7ae1 100644 --- a/freenect-server--http.c +++ b/freenect-server--http.c @@ -146,7 +146,9 @@ void connection_handler(int fd) { void *connection_thread(void *arg_anon) { int fd = (int)(intptr_t)arg_anon; + error(0, 0, "Connection %d opened", fd); connection_handler(fd); + error(0, 0, "Connection %d closed", fd); return NULL; } @@ -168,7 +170,8 @@ int main(int argc, char *argv[]) { for (int i = 2; i < argc; i++) file_add(argv[i]); - + + signal(SIGPIPE, SIG_IGN); while (1) { int conn = accept(sock, NULL, NULL); if (conn < 0) diff --git a/freenect-server--kinect.c b/freenect-server--kinect.c index 4573a08..32b029d 100644 --- a/freenect-server--kinect.c +++ b/freenect-server--kinect.c @@ -51,8 +51,10 @@ void dump_ffmpeg_pad16(FILE *stream, uint32_t timestamp UNUSED, void *data_anon, 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); + uint16_t z = *data; + uint8_t out[3]; + memset(out, (uint8_t)(z*2.0/3.0), 3); + fwrite(out, 3, 1, stream); data = &data[1]; } } diff --git a/freenect-server.sh b/freenect-server.sh index eebcd7f..1590eff 100644 --- a/freenect-server.sh +++ b/freenect-server.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash # Copyright 2016 Luke Shumaker -d="$(dirname -- "$0")" +PATH="$PATH:$(dirname -- "$0")" t="$(mktemp -d --tmpdir "${0##*/}.XXXXXXXXXX")" @@ -10,14 +10,15 @@ mkfifo $t/depth.rgb24 mkfifo $t/video.mjpg mkfifo $t/depth.mjpg -$d/freenect-server--kinect -v $t/video.rgb24 -d $t/depth.rgb24 & pids+=($!) -$d/freenect-server--http 8090 $t/video.mjpg $t/depth.mjpg & pids+=($!) -ffmpeg -loglevel warning -pix_fmt rgb24 -s 640x480 -f rawvideo -i $t/video.rgb24 -f mpjpeg - > $t/video.mjpg & pids+=($!) -ffmpeg -loglevel warning -pix_fmt rgb24 -s 640x480 -f rawvideo -i $t/depth.rgb24 -f mpjpeg - > $t/depth.mjpg & pids+=($!) +( freenect-server--kinect -v $t/video.rgb24 -d $t/depth.rgb24; echo "EXITED: freenect-server--kinect: $?") & pids+=($!) +( freenect-server--http 8090 $t/video.mjpg $t/depth.mjpg; echo "EXITED: freenect-server--http: $?") & pids+=($!) +rm -f {depth,video}.{avi,mjpg} +( ffmpeg -loglevel warning -pixel_format rgb24 -s 640x480 -f rawvideo -i $t/video.rgb24 -q:v 1 -f mpjpeg - > $t/video.mjpg; echo "EXITED: ffmpeg video: $?") & pids+=($!) +( ffmpeg -loglevel warning -pixel_format rgb24 -s 640x480 -f rawvideo -i $t/depth.rgb24 -q:v 1 -f mpjpeg - > $t/depth.mjpg; echo "EXITED: ffmpeg depth: $?") & pids+=($!) cleanup() { - kill -- "${pids[@]}" - rm -rf -- "$tmpdir" + kill -- "${pids[@]}" 2>/dev/null + rm -rf -- "$t" } trap cleanup EXIT diff --git a/mpjpeg.c b/mpjpeg.c index b64c5f0..87ca2a4 100644 --- a/mpjpeg.c +++ b/mpjpeg.c @@ -1,10 +1,11 @@ /* Copyright 2016 Luke Shumaker */ +#include +#include #include #include #include #include -#include #include "mpjpeg.h" #include "util.h" @@ -47,14 +48,16 @@ void mpjpeg_reader(struct mpjpeg_stream *s, int fd, const char *boundary) { while (1) { s->back->len = -1; - /* read the frame header (MIME headers) */ + /* scan for the first non-empty line */ do { line_len = getline(&line_buf, &line_cap, stream); } while (line_len >= 0 && strcmp(line_buf, "\r\n") == 0); + /* make sure it matches the boundary separator */ if (strcmp(line_buf, boundary) != 0) { error(0, 0, "line does not match boundary: \"%s\"", line_buf); return; } + /* read the frame header (MIME headers) */ while (strcmp(line_buf, "\r\n") != 0 && line_len >= 0) { line_len = getline(&line_buf, &line_cap, stream); if (strncasecmp(line_buf, "Content-length:", strlen("Content-length:")) == 0) { @@ -75,35 +78,44 @@ void mpjpeg_reader(struct mpjpeg_stream *s, int fd, const char *boundary) { } /* swap the frames */ - pthread_mutex_lock(&s->frontlock); + pthread_rwlock_wrlock(&s->frontlock); struct frame *tmp = s->front; s->front = s->back; s->back = tmp; - pthread_mutex_unlock(&s->frontlock); + s->framecount++; + pthread_rwlock_unlock(&s->frontlock); } } void mpjpeg_writer(struct mpjpeg_stream *s, int fd, const char *boundary) { struct frame myframe = { 0 }; - struct frame *lastframe = NULL; + long lastframe = 0; + pthread_rwlock_rdlock(&s->frontlock); while(1) { /* get the most recent frame (copy front to myframe) */ - pthread_mutex_lock(&s->frontlock); - if (s->front == lastframe) { /* TODO: use a pthread_cond_t instead of a busy loop */ - pthread_mutex_unlock(&s->frontlock); - continue; - } if (myframe.cap < (size_t)s->front->len) myframe.data = xrealloc(myframe.data, myframe.cap = s->front->len); memcpy(myframe.data, s->front->data, myframe.len = s->front->len); - lastframe = s->front; - pthread_mutex_unlock(&s->frontlock); + lastframe = s->framecount; + pthread_rwlock_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) { + error(0, errno, "dprintf"); return; - if (write(fd, myframe.data, myframe.len) < myframe.len) + } + if (write(fd, myframe.data, myframe.len) < myframe.len) { + error(0, errno, "write"); return; + } + + /* poll until there's a new frame */ + pthread_rwlock_rdlock(&s->frontlock); + while (s->framecount == lastframe) { + pthread_rwlock_unlock(&s->frontlock); + usleep(30000); /* a bit over 30 FPS */ + pthread_rwlock_rdlock(&s->frontlock); + } } } @@ -112,5 +124,5 @@ void init_mpjpeg_stream(struct mpjpeg_stream *s) { ZERO(s->b); s->front = &s->a; s->back = &s->b; - pthread_mutex_init(&s->frontlock, NULL); + pthread_rwlock_init(&s->frontlock, NULL); } diff --git a/mpjpeg.h b/mpjpeg.h index 85f4fcf..f850adc 100644 --- a/mpjpeg.h +++ b/mpjpeg.h @@ -11,12 +11,10 @@ struct frame { }; struct mpjpeg_stream { - struct frame a; - struct frame b; - - struct frame *front; - struct frame *back; - pthread_mutex_t frontlock; + struct frame a, b; + struct frame *front, *back; + pthread_rwlock_t frontlock; + long framecount; }; void mpjpeg_reader(struct mpjpeg_stream *s, int fd, const char *boundary); -- cgit v1.2.3