diff options
Diffstat (limited to 'thread_mpjpeg.c')
-rw-r--r-- | thread_mpjpeg.c | 33 |
1 files changed, 21 insertions, 12 deletions
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; |