-
Notifications
You must be signed in to change notification settings - Fork 1
/
mjpg_streamer.c
427 lines (380 loc) · 15.4 KB
/
mjpg_streamer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*******************************************************************************
# #
# MJPG-streamer allows to stream JPG frames from an input-plugin #
# to several output plugins #
# #
# Copyright (C) 2007 Tom Stöveken #
# #
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; version 2 of the License. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the Free Software #
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #
# #
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <linux/videodev2.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <getopt.h>
#include <pthread.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <syslog.h>
#include "utils.h"
#include "mjpg_streamer.h"
/* globals */
static globals global;
/******************************************************************************
Description.: Display a help message
Input Value.: argv[0] is the program name and the parameter progname
Return Value: -
******************************************************************************/
void help(char *progname)
{
fprintf(stderr, "-----------------------------------------------------------------------\n");
fprintf(stderr, "Usage: %s\n" \
" -i | --input \"<input-plugin.so> [parameters]\"\n" \
" -o | --output \"<output-plugin.so> [parameters]\"\n" \
" [-h | --help ]........: display this help\n" \
" [-v | --version ].....: display version information\n" \
" [-b | --background]...: fork to the background, daemon mode\n", progname);
fprintf(stderr, "-----------------------------------------------------------------------\n");
fprintf(stderr, "Example #1:\n" \
" To open an UVC webcam \"/dev/video1\" and stream it via HTTP:\n" \
" %s -i \"input_uvc.so -d /dev/video1\" -o \"output_http.so\"\n", progname);
fprintf(stderr, "-----------------------------------------------------------------------\n");
fprintf(stderr, "Example #2:\n" \
" To open an UVC webcam and stream via HTTP port 8090:\n" \
" %s -i \"input_uvc.so\" -o \"output_http.so -p 8090\"\n", progname);
fprintf(stderr, "-----------------------------------------------------------------------\n");
fprintf(stderr, "Example #3:\n" \
" To get help for a certain input plugin:\n" \
" %s -i \"input_uvc.so --help\"\n", progname);
fprintf(stderr, "-----------------------------------------------------------------------\n");
fprintf(stderr, "In case the modules (=plugins) can not be found:\n" \
" * Set the default search path for the modules with:\n" \
" export LD_LIBRARY_PATH=/path/to/plugins,\n" \
" * or put the plugins into the \"/lib/\" or \"/usr/lib\" folder,\n" \
" * or instead of just providing the plugin file name, use a complete\n" \
" path and filename:\n" \
" %s -i \"/path/to/modules/input_uvc.so\"\n", progname);
fprintf(stderr, "-----------------------------------------------------------------------\n");
}
/******************************************************************************
Description.: pressing CTRL+C sends signals to this process instead of just
killing it plugins can tidily shutdown and free allocated
resources. The function prototype is defined by the system,
because it is a callback function.
Input Value.: sig tells us which signal was received
Return Value: -
******************************************************************************/
void signal_handler(int sig)
{
int i;
/* signal "stop" to threads */
LOG("setting signal to stop\n");
global.stop = 1;
usleep(1000 * 1000);
/* clean up threads */
LOG("force cancellation of threads and cleanup resources\n");
for(i = 0; i < global.incnt; i++) {
global.in[i].stop(i);
}
for(i = 0; i < global.outcnt; i++) {
global.out[i].stop(global.out[i].param.id);
pthread_cond_destroy(&global.in[i].db_update);
pthread_mutex_destroy(&global.in[i].db);
}
usleep(1000 * 1000);
/* close handles of input plugins */
for(i = 0; i < global.incnt; i++) {
dlclose(global.in[i].handle);
}
for(i = 0; i < global.outcnt; i++) {
/* skip = 0;
DBG("about to decrement usage counter for handle of %s, id #%02d, handle: %p\n", \
global.out[i].plugin, global.out[i].param.id, global.out[i].handle);
for(j=i+1; j<global.outcnt; j++) {
if ( global.out[i].handle == global.out[j].handle ) {
DBG("handles are pointing to the same destination (%p == %p)\n", global.out[i].handle, global.out[j].handle);
skip = 1;
}
}
if ( skip ) {
continue;
}
DBG("closing handle %p\n", global.out[i].handle);
*/
dlclose(global.out[i].handle);
}
DBG("all plugin handles closed\n");
LOG("done\n");
closelog();
exit(0);
return;
}
int split_parameters(char *parameter_string, int *argc, char **argv)
{
int count = 1;
argv[0] = NULL; // the plugin may set it to 'INPUT_PLUGIN_NAME'
if(parameter_string != NULL && strlen(parameter_string) != 0) {
char *arg = NULL, *saveptr = NULL, *token = NULL;
arg = strdup(parameter_string);
if(strchr(arg, ' ') != NULL) {
token = strtok_r(arg, " ", &saveptr);
if(token != NULL) {
argv[count] = strdup(token);
count++;
while((token = strtok_r(NULL, " ", &saveptr)) != NULL) {
argv[count] = strdup(token);
count++;
if(count >= MAX_PLUGIN_ARGUMENTS) {
IPRINT("ERROR: too many arguments to input plugin\n");
return 0;
}
}
}
}
}
*argc = count;
return 1;
}
/******************************************************************************
Description.:
Input Value.:
Return Value:
******************************************************************************/
int main(int argc, char *argv[])
{
//char *input = "input_uvc.so --resolution 640x480 --fps 5 --device /dev/video0";
char *input[MAX_INPUT_PLUGINS];
char *output[MAX_OUTPUT_PLUGINS];
int daemon = 0, i;
size_t tmp = 0;
output[0] = "output_http.so --port 8080";
global.outcnt = 0;
/* parameter parsing */
while(1) {
int option_index = 0, c = 0;
static struct option long_options[] = {
{"h", no_argument, 0, 0
},
{"help", no_argument, 0, 0},
{"i", required_argument, 0, 0},
{"input", required_argument, 0, 0},
{"o", required_argument, 0, 0},
{"output", required_argument, 0, 0},
{"v", no_argument, 0, 0},
{"version", no_argument, 0, 0},
{"b", no_argument, 0, 0},
{"background", no_argument, 0, 0},
{0, 0, 0, 0}
};
c = getopt_long_only(argc, argv, "", long_options, &option_index);
/* no more options to parse */
if(c == -1) break;
/* unrecognized option */
if(c == '?') {
help(argv[0]);
return 0;
}
switch(option_index) {
/* h, help */
case 0:
case 1:
help(argv[0]);
return 0;
break;
/* i, input */
case 2:
case 3:
input[global.incnt++] = strdup(optarg);
break;
/* o, output */
case 4:
case 5:
output[global.outcnt++] = strdup(optarg);
break;
/* v, version */
case 6:
case 7:
printf("MJPG Streamer Version: %s\n" \
"Compilation Date.....: %s\n" \
"Compilation Time.....: %s\n",
#ifdef SVN_REV
SVN_REV,
#else
SOURCE_VERSION,
#endif
__DATE__, __TIME__);
return 0;
break;
/* b, background */
case 8:
case 9:
daemon = 1;
break;
default:
help(argv[0]);
return 0;
}
}
openlog("MJPG-streamer ", LOG_PID | LOG_CONS, LOG_USER);
//openlog("MJPG-streamer ", LOG_PID|LOG_CONS|LOG_PERROR, LOG_USER);
syslog(LOG_INFO, "starting application");
/* fork to the background */
if(daemon) {
LOG("enabling daemon mode");
daemon_mode();
}
/* ignore SIGPIPE (send by OS if transmitting to closed TCP sockets) */
signal(SIGPIPE, SIG_IGN);
/* register signal handler for <CTRL>+C in order to clean up */
if(signal(SIGINT, signal_handler) == SIG_ERR) {
LOG("could not register signal handler\n");
closelog();
exit(EXIT_FAILURE);
}
/*
* messages like the following will only be visible on your terminal
* if not running in daemon mode
*/
#ifdef SVN_REV
LOG("MJPG Streamer Version: svn rev: %s\n", SVN_REV);
#else
LOG("MJPG Streamer Version.: %s\n", SOURCE_VERSION);
#endif
/* check if at least one output plugin was selected */
if(global.outcnt == 0) {
/* no? Then use the default plugin instead */
global.outcnt = 1;
}
/* open input plugin */
for(i = 0; i < global.incnt; i++) {
/* this mutex and the conditional variable are used to synchronize access to the global picture buffer */
if(pthread_mutex_init(&global.in[i].db, NULL) != 0) {
LOG("could not initialize mutex variable\n");
closelog();
exit(EXIT_FAILURE);
}
if(pthread_cond_init(&global.in[i].db_update, NULL) != 0) {
LOG("could not initialize condition variable\n");
closelog();
exit(EXIT_FAILURE);
}
tmp = (size_t)(strchr(input[i], ' ') - input[i]);
global.in[i].stop = 0;
global.in[i].buf = NULL;
global.in[i].size = 0;
global.in[i].plugin = (tmp > 0) ? strndup(input[i], tmp) : strdup(input[i]);
global.in[i].handle = dlopen(global.in[i].plugin, RTLD_LAZY);
if(!global.in[i].handle) {
LOG("ERROR: could not find input plugin\n");
LOG(" Perhaps you want to adjust the search path with:\n");
LOG(" # export LD_LIBRARY_PATH=/path/to/plugin/folder\n");
LOG(" dlopen: %s\n", dlerror());
closelog();
exit(EXIT_FAILURE);
}
global.in[i].init = dlsym(global.in[i].handle, "input_init");
if(global.in[i].init == NULL) {
LOG("%s\n", dlerror());
exit(EXIT_FAILURE);
}
global.in[i].stop = dlsym(global.in[i].handle, "input_stop");
if(global.in[i].stop == NULL) {
LOG("%s\n", dlerror());
exit(EXIT_FAILURE);
}
global.in[i].run = dlsym(global.in[i].handle, "input_run");
if(global.in[i].run == NULL) {
LOG("%s\n", dlerror());
exit(EXIT_FAILURE);
}
/* try to find optional command */
global.in[i].cmd = dlsym(global.in[i].handle, "input_cmd");
global.in[i].param.parameters = strchr(input[i], ' ');
split_parameters(global.in[i].param.parameters, &global.in[i].param.argc, global.in[i].param.argv);
global.in[i].param.global = &global;
global.in[i].param.id = i;
if(global.in[i].init(&global.in[i].param, i)) {
LOG("input_init() return value signals to exit\n");
closelog();
exit(0);
}
}
/* open output plugin */
for(i = 0; i < global.outcnt; i++) {
tmp = (size_t)(strchr(output[i], ' ') - output[i]);
global.out[i].plugin = (tmp > 0) ? strndup(output[i], tmp) : strdup(output[i]);
global.out[i].handle = dlopen(global.out[i].plugin, RTLD_LAZY);
if(!global.out[i].handle) {
LOG("ERROR: could not find output plugin %s\n", global.out[i].plugin);
LOG(" Perhaps you want to adjust the search path with:\n");
LOG(" # export LD_LIBRARY_PATH=/path/to/plugin/folder\n");
LOG(" dlopen: %s\n", dlerror());
closelog();
exit(EXIT_FAILURE);
}
global.out[i].init = dlsym(global.out[i].handle, "output_init");
if(global.out[i].init == NULL) {
LOG("%s\n", dlerror());
exit(EXIT_FAILURE);
}
global.out[i].stop = dlsym(global.out[i].handle, "output_stop");
if(global.out[i].stop == NULL) {
LOG("%s\n", dlerror());
exit(EXIT_FAILURE);
}
global.out[i].run = dlsym(global.out[i].handle, "output_run");
if(global.out[i].run == NULL) {
LOG("%s\n", dlerror());
exit(EXIT_FAILURE);
}
/* try to find optional command */
global.out[i].cmd = dlsym(global.out[i].handle, "output_cmd");
global.out[i].param.parameters = strchr(output[i], ' ');
split_parameters(global.out[i].param.parameters, &global.out[i].param.argc, global.out[i].param.argv);
global.out[i].param.global = &global;
global.out[i].param.id = i;
if(global.out[i].init(&global.out[i].param, i)) {
LOG("output_init() return value signals to exit\n");
closelog();
exit(0);
}
}
/* start to read the input, push pictures into global buffer */
DBG("starting %d input plugin\n", global.incnt);
for(i = 0; i < global.incnt; i++) {
syslog(LOG_INFO, "starting input plugin %s", global.in[i].plugin);
if(global.in[i].run(i)) {
LOG("can not run input plugin %d: %s\n", i, global.in[i].plugin);
closelog();
return 1;
}
}
DBG("starting %d output plugin(s)\n", global.outcnt);
for(i = 0; i < global.outcnt; i++) {
syslog(LOG_INFO, "starting output plugin: %s (ID: %02d)", global.out[i].plugin, global.out[i].param.id);
global.out[i].run(global.out[i].param.id);
}
/* wait for signals */
pause();
return 0;
}