/* Copyright 2016 Luke Shumaker */ #include #include #include #include #include #include #include "mpjpeg.h" #include "util.h" static char *boundary_line(const char *old) { size_t len = strlen(old); 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; } static ssize_t safe_atoi(char *str) { while (*str == ' ') str++; size_t len = 0; while ('0' <= str[len] && str[len] <= '9') len++; size_t i = len; while (str[i] == ' ') i++; if (len < 1 || strcmp(&str[i], "\r\n") != 0) return -1; return atoi(str); } void mpjpeg_reader(struct mpjpeg_stream *s, int fd, const char *boundary) { FILE *stream = fdopen(fd, "r"); boundary = boundary_line(boundary); char *line_buf = NULL; size_t line_cap = 0; ssize_t line_len = 0; while (1) { s->back->len = -1; /* 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) { s->back->len = safe_atoi(&line_buf[strlen("Content-length:")]); } } if (s->back->len < 0) { error(0, 0, "did not get frame length"); return; } /* read the frame contents (JPEG) */ if (s->back->cap < (size_t)s->back->len) s->back->data = xrealloc(s->back->data, s->back->cap = s->back->len); if (fread(s->back->data, s->back->len, 1, stream) != 1) { error(0, ferror(stream), "fread(%zd)", s->back->len); return; } /* swap the frames */ pthread_rwlock_wrlock(&s->frontlock); struct frame *tmp = s->front; s->front = s->back; s->back = tmp; s->framecount++; pthread_rwlock_unlock(&s->frontlock); } } void mpjpeg_writer(struct mpjpeg_stream *s, int fd, const char *boundary) { struct frame myframe = { 0 }; long lastframe = 0; pthread_rwlock_rdlock(&s->frontlock); while(1) { /* get the most recent frame (copy front to myframe) */ 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->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) { error(0, errno, "dprintf"); return; } 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); } } } void init_mpjpeg_stream(struct mpjpeg_stream *s) { ZERO(s->a); ZERO(s->b); s->front = &s->a; s->back = &s->b; pthread_rwlock_init(&s->frontlock, NULL); }