diff options
author | Luke Shumaker <lukeshu@sbcglobal.net> | 2016-03-12 02:51:12 -0500 |
---|---|---|
committer | Luke Shumaker <lukeshu@sbcglobal.net> | 2016-03-12 02:51:12 -0500 |
commit | e4dce6b0d0a465f792d1de70c0c65c2fcb8d196c (patch) | |
tree | 746e15f857fada214c54d53c15aeba90899e9bd5 /mpjpeg.c | |
parent | bd9887f10533eba76129f14f92ac971f11d1b1c0 (diff) |
fixity fix
Diffstat (limited to 'mpjpeg.c')
-rw-r--r-- | mpjpeg.c | 42 |
1 files changed, 27 insertions, 15 deletions
@@ -1,10 +1,11 @@ /* Copyright 2016 Luke Shumaker */ +#include <errno.h> +#include <error.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> -#include <error.h> #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); } |