mjpg_streamer.c
Go to the documentation of this file.
00001 #include <sys/stat.h>
00002 #include <stdio.h>
00003 #include <string.h>
00004 #include <stdlib.h>
00005 #include <time.h>
00006 #include "mongoose.h"
00007 
00008 static void send_file(struct mg_connection *conn, const char *path) {
00009   char buf[1024];
00010   struct stat st;
00011   int n;
00012   FILE *fp;
00013 
00014   if (stat(path, &st) == 0 && (fp = fopen(path, "rb")) != NULL) {
00015     mg_printf(conn, "--w00t\r\nContent-Type: image/jpeg\r\n"
00016               "Content-Length: %lu\r\n\r\n", (unsigned long) st.st_size);
00017     while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) {
00018       mg_write(conn, buf, n);
00019     }
00020     fclose(fp);
00021     mg_write(conn, "\r\n", 2);
00022   }
00023 }
00024 
00025 struct conn_state {
00026   int file_index;
00027   time_t last_poll;
00028 };
00029 
00030 static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
00031   const char **file_names = (const char **) conn->server_param;
00032   struct conn_state *state;
00033   time_t now = time(NULL);
00034 
00035   switch (ev) {
00036 
00037     case MG_AUTH:
00038       return MG_TRUE;
00039 
00040     case MG_REQUEST:
00041       if (strcmp(conn->uri, "/stream") != 0) {
00042         mg_send_header(conn, "Content-Type", "text/html");
00043         mg_printf_data(conn, "%s",
00044                        "Go to <a href=/stream>/stream</a> for MJPG stream");
00045         return MG_TRUE;
00046       }
00047 
00048       mg_printf(conn, "%s",
00049                 "HTTP/1.0 200 OK\r\n" "Cache-Control: no-cache\r\n"
00050                 "Pragma: no-cache\r\nExpires: Thu, 01 Dec 1994 16:00:00 GMT\r\n"
00051                 "Connection: close\r\nContent-Type: multipart/x-mixed-replace; "
00052                 "boundary=--w00t\r\n\r\n");
00053 
00054       send_file(conn, file_names[0]);
00055 
00056       state = (struct conn_state *) malloc(sizeof(*state));
00057       conn->connection_param = state;
00058       state->file_index = 1;  // First file is already sent
00059       state->last_poll = time(NULL);
00060       return MG_MORE;
00061 
00062     case MG_POLL:
00063       state = (struct conn_state *) conn->connection_param;
00064 
00065       if (state != NULL && now > state->last_poll) {
00066         if (file_names[state->file_index] != NULL) {
00067           send_file(conn, file_names[state->file_index]);
00068           state->file_index++;
00069           if (file_names[state->file_index] == NULL) {
00070             return MG_TRUE;  // No more images, close connection
00071           }
00072         }
00073         state->last_poll = now;
00074       }
00075       return MG_FALSE;
00076 
00077     case MG_CLOSE:
00078       free(conn->connection_param);
00079       conn->connection_param = NULL;
00080       return MG_FALSE;
00081 
00082     default:
00083       return MG_FALSE;
00084   }
00085 }
00086 
00087 int main(int argc, char *argv[]) {
00088   struct mg_server *server;
00089 
00090   if (argc < 3) {
00091     printf("Usage: %s image1.jpg image2.jpg ...\n", argv[0]);
00092     return 1;
00093   }
00094 
00095   server = mg_create_server(&argv[1], ev_handler);
00096   mg_set_option(server, "listening_port", "8080");
00097 
00098   printf("Starting on port %s\n", mg_get_option(server, "listening_port"));
00099   for (;;) {
00100     mg_poll_server(server, 1000);
00101   }
00102   mg_destroy_server(&server);
00103 
00104   return 0;
00105 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:05