From dbc07c8b00476fee47ac4739a79d63a9858ae593 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Mon, 15 Sep 2014 19:05:30 +0100 Subject: [PATCH] Moved mjpg_streamer to a separate dir --- examples/mjpg.c | 94 ---------------------- examples/mjpg_streamer/Makefile | 12 +++ examples/mjpg_streamer/mjpg_streamer.c | 105 +++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 94 deletions(-) delete mode 100644 examples/mjpg.c create mode 100644 examples/mjpg_streamer/Makefile create mode 100644 examples/mjpg_streamer/mjpg_streamer.c diff --git a/examples/mjpg.c b/examples/mjpg.c deleted file mode 100644 index 1fe26288..00000000 --- a/examples/mjpg.c +++ /dev/null @@ -1,94 +0,0 @@ -#include -#include -#include -#include -#include -#include "mongoose.h" - -static void send_file(struct mg_connection *conn, const char *path) { - char buf[1024]; - struct stat st; - int n; - FILE *fp; - - if (stat(path, &st) == 0 && (fp = fopen(path, "rb")) != NULL) { - mg_printf(conn, "--myboundary\r\nContent-Type: image/jpeg\r\n" - "Content-Length: %llu\r\n\r\n", (unsigned long long) st.st_size); - while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) { - mg_write(conn, buf, n); - } - fclose(fp); - mg_write(conn, "\r\n", 2); - } -} - -struct conn_state { - int file_index; - time_t last_poll; -}; - -static int ev_handler(struct mg_connection *conn, enum mg_event ev) { - const char **file_names = (const char **) conn->server_param; - struct conn_state *state; - int result = MG_FALSE; - - if (ev == MG_REQUEST) { - mg_printf(conn, "%s", - "HTTP/1.0 200 OK\r\n" "Cache-Control: no-cache\r\n" - "Pragma: no-cache\r\nExpires: Thu, 01 Dec 1994 16:00:00 GMT\r\n" - "Connection: close\r\nContent-Type: multipart/x-mixed-replace; " - "boundary=--myboundary\r\n\r\n"); - - send_file(conn, file_names[0]); - - state = (struct conn_state *) malloc(sizeof(*state)); - conn->connection_param = state; - state->file_index = 1; // First file is already sent - state->last_poll = time(NULL); - result = MG_MORE; - - } else if (ev == MG_POLL) { - - time_t now = time(NULL); - state = (struct conn_state *) conn->connection_param; - - if (state != NULL && now > state->last_poll) { - if (file_names[state->file_index] != NULL) { - send_file(conn, file_names[state->file_index]); - state->file_index++; - if (file_names[state->file_index] == NULL) { - result = MG_TRUE; // No more images, close connection - } - } - state->last_poll = now; - } - - } else if (ev == MG_CLOSE) { - free(conn->connection_param); - conn->connection_param = NULL; - } else if (ev == MG_AUTH) { - result = MG_TRUE; - } - - return result; -} - -int main(int argc, char *argv[]) { - struct mg_server *server; - - if (argc < 3) { - printf("Usage: %s image1.jpg image2.jpg ...\n", argv[0]); - return 1; - } - - server = mg_create_server(&argv[1], ev_handler); - mg_set_option(server, "listening_port", "8080"); - - printf("Starting on port %s\n", mg_get_option(server, "listening_port")); - for (;;) { - mg_poll_server(server, 1000); - } - mg_destroy_server(&server); - - return 0; -} diff --git a/examples/mjpg_streamer/Makefile b/examples/mjpg_streamer/Makefile new file mode 100644 index 00000000..0d16814a --- /dev/null +++ b/examples/mjpg_streamer/Makefile @@ -0,0 +1,12 @@ +# Copyright (c) 2014 Cesanta Software +# All rights reserved + +PROG = mjpg_streamer +CFLAGS = -W -Wall -I../.. -g -O0 $(CFLAGS_EXTRA) +SOURCES = $(PROG).c ../../mongoose.c + +$(PROG): $(SOURCES) + $(CC) -o $(PROG) $(SOURCES) $(CFLAGS) + +clean: + rm -rf $(PROG) *.exe *.dSYM *.obj *.exp .*o *.lib diff --git a/examples/mjpg_streamer/mjpg_streamer.c b/examples/mjpg_streamer/mjpg_streamer.c new file mode 100644 index 00000000..4bac6c14 --- /dev/null +++ b/examples/mjpg_streamer/mjpg_streamer.c @@ -0,0 +1,105 @@ +#include +#include +#include +#include +#include +#include "mongoose.h" + +static void send_file(struct mg_connection *conn, const char *path) { + char buf[1024]; + struct stat st; + int n; + FILE *fp; + + if (stat(path, &st) == 0 && (fp = fopen(path, "rb")) != NULL) { + mg_printf(conn, "--w00t\r\nContent-Type: image/jpeg\r\n" + "Content-Length: %lu\r\n\r\n", (unsigned long) st.st_size); + while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) { + mg_write(conn, buf, n); + } + fclose(fp); + mg_write(conn, "\r\n", 2); + } +} + +struct conn_state { + int file_index; + time_t last_poll; +}; + +static int ev_handler(struct mg_connection *conn, enum mg_event ev) { + const char **file_names = (const char **) conn->server_param; + struct conn_state *state; + time_t now = time(NULL); + + switch (ev) { + + case MG_AUTH: + return MG_TRUE; + + case MG_REQUEST: + if (strcmp(conn->uri, "/stream") != 0) { + mg_send_header(conn, "Content-Type", "text/html"); + mg_printf_data(conn, "%s", + "Go to /stream for MJPG stream"); + return MG_TRUE; + } + + mg_printf(conn, "%s", + "HTTP/1.0 200 OK\r\n" "Cache-Control: no-cache\r\n" + "Pragma: no-cache\r\nExpires: Thu, 01 Dec 1994 16:00:00 GMT\r\n" + "Connection: close\r\nContent-Type: multipart/x-mixed-replace; " + "boundary=--w00t\r\n\r\n"); + + send_file(conn, file_names[0]); + + state = (struct conn_state *) malloc(sizeof(*state)); + conn->connection_param = state; + state->file_index = 1; // First file is already sent + state->last_poll = time(NULL); + return MG_MORE; + + case MG_POLL: + state = (struct conn_state *) conn->connection_param; + + if (state != NULL && now > state->last_poll) { + if (file_names[state->file_index] != NULL) { + send_file(conn, file_names[state->file_index]); + state->file_index++; + if (file_names[state->file_index] == NULL) { + return MG_TRUE; // No more images, close connection + } + } + state->last_poll = now; + } + return MG_FALSE; + + case MG_CLOSE: + free(conn->connection_param); + conn->connection_param = NULL; + return MG_FALSE; + + default: + return MG_FALSE; + } +} + +int main(int argc, char *argv[]) { + struct mg_server *server; + + if (argc < 3) { + printf("Usage: %s image1.jpg image2.jpg ...\n", argv[0]); + return 1; + } + + server = mg_create_server(&argv[1], ev_handler); + mg_set_option(server, "listening_port", "8080"); + + printf("Starting on port %s\n", mg_get_option(server, "listening_port")); + for (;;) { + mg_poll_server(server, 1000); + } + mg_destroy_server(&server); + + return 0; +}