mjpg_streamer.c
Go to the documentation of this file.
1 #include <sys/stat.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <time.h>
6 #include "mongoose.h"
7 
8 static void send_file(struct mg_connection *conn, const char *path) {
9  char buf[1024];
10  struct stat st;
11  int n;
12  FILE *fp;
13 
14  if (stat(path, &st) == 0 && (fp = fopen(path, "rb")) != NULL) {
15  mg_printf(conn, "--w00t\r\nContent-Type: image/jpeg\r\n"
16  "Content-Length: %lu\r\n\r\n", (unsigned long) st.st_size);
17  while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) {
18  mg_write(conn, buf, n);
19  }
20  fclose(fp);
21  mg_write(conn, "\r\n", 2);
22  }
23 }
24 
25 struct conn_state {
27  time_t last_poll;
28 };
29 
30 static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
31  const char **file_names = (const char **) conn->server_param;
32  struct conn_state *state;
33  time_t now = time(NULL);
34 
35  switch (ev) {
36 
37  case MG_AUTH:
38  return MG_TRUE;
39 
40  case MG_REQUEST:
41  if (strcmp(conn->uri, "/stream") != 0) {
42  mg_send_header(conn, "Content-Type", "text/html");
43  mg_printf_data(conn, "%s",
44  "Go to <a href=/stream>/stream</a> for MJPG stream");
45  return MG_TRUE;
46  }
47 
48  mg_printf(conn, "%s",
49  "HTTP/1.0 200 OK\r\n" "Cache-Control: no-cache\r\n"
50  "Pragma: no-cache\r\nExpires: Thu, 01 Dec 1994 16:00:00 GMT\r\n"
51  "Connection: close\r\nContent-Type: multipart/x-mixed-replace; "
52  "boundary=--w00t\r\n\r\n");
53 
54  send_file(conn, file_names[0]);
55 
56  state = (struct conn_state *) malloc(sizeof(*state));
57  conn->connection_param = state;
58  state->file_index = 1; // First file is already sent
59  state->last_poll = time(NULL);
60  return MG_MORE;
61 
62  case MG_POLL:
63  state = (struct conn_state *) conn->connection_param;
64 
65  if (state != NULL && now > state->last_poll) {
66  if (file_names[state->file_index] != NULL) {
67  send_file(conn, file_names[state->file_index]);
68  state->file_index++;
69  if (file_names[state->file_index] == NULL) {
70  return MG_TRUE; // No more images, close connection
71  }
72  }
73  state->last_poll = now;
74  }
75  return MG_FALSE;
76 
77  case MG_CLOSE:
78  free(conn->connection_param);
79  conn->connection_param = NULL;
80  return MG_FALSE;
81 
82  default:
83  return MG_FALSE;
84  }
85 }
86 
87 int main(int argc, char *argv[]) {
88  struct mg_server *server;
89 
90  if (argc < 3) {
91  printf("Usage: %s image1.jpg image2.jpg ...\n", argv[0]);
92  return 1;
93  }
94 
95  server = mg_create_server(&argv[1], ev_handler);
96  mg_set_option(server, "listening_port", "8080");
97 
98  printf("Starting on port %s\n", mg_get_option(server, "listening_port"));
99  for (;;) {
100  mg_poll_server(server, 1000);
101  }
102  mg_destroy_server(&server);
103 
104  return 0;
105 }
#define free(ptr)
Definition: curl_memory.h:130
#define state(x, y)
Definition: ftp.c:100
void mg_send_header(struct mg_connection *c, const char *name, const char *v)
Definition: mongoose.c:2759
void * connection_param
Definition: mongoose.h:56
int stat(const char *path, struct stat *buffer)
const char * uri
Definition: mongoose.h:34
struct curltime now
Definition: unit1399.c:83
struct mg_server * mg_create_server(void *server_data, mg_handler_t handler)
Definition: mongoose.c:5431
const char * mg_get_option(const struct mg_server *server, const char *name)
Definition: mongoose.c:5425
if(strcmp(arg,"1305")!=0)
Definition: unit1305.c:127
#define malloc(size)
Definition: curl_memory.h:124
size_t mg_write(struct mg_connection *c, const void *buf, size_t len)
Definition: mongoose.c:2744
time_t mg_poll_server(struct mg_server *server, int milliseconds)
Definition: mongoose.c:4965
size_t mg_printf_data(struct mg_connection *c, const char *fmt,...)
Definition: mongoose.c:2785
#define printf
Definition: curl_printf.h:40
mg_event
Definition: mongoose.h:62
static struct mg_server * server
Definition: web_server.c:72
time_t last_poll
Definition: mjpg_streamer.c:27
int main(int argc, char *argv[])
Definition: mjpg_streamer.c:87
size_t fread(void *, size_t, size_t, FILE *)
static void send_file(struct mg_connection *conn, const char *path)
Definition: mjpg_streamer.c:8
static int ev_handler(struct mg_connection *conn, enum mg_event ev)
Definition: mjpg_streamer.c:30
char buf[3]
Definition: unit1398.c:32
const char * mg_set_option(struct mg_server *server, const char *name, const char *value)
Definition: mongoose.c:5143
size_t mg_printf(struct mg_connection *conn, const char *fmt,...)
Definition: mongoose.c:1949
void mg_destroy_server(struct mg_server **server)
Definition: mongoose.c:4969
const char * path
Definition: util.c:192
void * server_param
Definition: mongoose.h:55


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:15