From e4dce6b0d0a465f792d1de70c0c65c2fcb8d196c Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sat, 12 Mar 2016 02:51:12 -0500 Subject: fixity fix --- mpjpeg.c | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) (limited to 'mpjpeg.c') 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); } -- cgit v1.2.3-54-g00ecf