]> git.sesse.net Git - ffmpeg/blob - ffserver_config.c
Merge commit 'fbd6c97f9ca858140df16dd07200ea0d4bdc1a83'
[ffmpeg] / ffserver_config.c
1 /*
2  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include <float.h>
22 #include "libavutil/opt.h"
23 #include "libavutil/parseutils.h"
24 #include "libavutil/avstring.h"
25 #include "libavutil/pixdesc.h"
26 #include "libavutil/avassert.h"
27
28 // FIXME those are internal headers, ffserver _really_ shouldn't use them
29 #include "libavformat/ffm.h"
30
31 #include "cmdutils.h"
32 #include "ffserver_config.h"
33
34 #define MAX_CHILD_ARGS 64
35
36 static int ffserver_save_avoption(const char *opt, const char *arg, int type,
37                                   FFServerConfig *config);
38 static void vreport_config_error(const char *filename, int line_num, int log_level,
39                                  int *errors, const char *fmt, va_list vl);
40 static void report_config_error(const char *filename, int line_num, int log_level,
41                                 int *errors, const char *fmt, ...);
42
43 /* FIXME: make ffserver work with IPv6 */
44 /* resolve host with also IP address parsing */
45 static int resolve_host(struct in_addr *sin_addr, const char *hostname)
46 {
47
48     if (!ff_inet_aton(hostname, sin_addr)) {
49 #if HAVE_GETADDRINFO
50         struct addrinfo *ai, *cur;
51         struct addrinfo hints = { 0 };
52         hints.ai_family = AF_INET;
53         if (getaddrinfo(hostname, NULL, &hints, &ai))
54             return -1;
55         /* getaddrinfo returns a linked list of addrinfo structs.
56          * Even if we set ai_family = AF_INET above, make sure
57          * that the returned one actually is of the correct type. */
58         for (cur = ai; cur; cur = cur->ai_next) {
59             if (cur->ai_family == AF_INET) {
60                 *sin_addr = ((struct sockaddr_in *)cur->ai_addr)->sin_addr;
61                 freeaddrinfo(ai);
62                 return 0;
63             }
64         }
65         freeaddrinfo(ai);
66         return -1;
67 #else
68         struct hostent *hp;
69         hp = gethostbyname(hostname);
70         if (!hp)
71             return -1;
72         memcpy(sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
73 #endif
74     }
75     return 0;
76 }
77
78 void ffserver_get_arg(char *buf, int buf_size, const char **pp)
79 {
80     const char *p;
81     char *q;
82     int quote;
83
84     p = *pp;
85     while (av_isspace(*p)) p++;
86     q = buf;
87     quote = 0;
88     if (*p == '\"' || *p == '\'')
89         quote = *p++;
90     for(;;) {
91         if (quote) {
92             if (*p == quote)
93                 break;
94         } else {
95             if (av_isspace(*p))
96                 break;
97         }
98         if (*p == '\0')
99             break;
100         if ((q - buf) < buf_size - 1)
101             *q++ = *p;
102         p++;
103     }
104     *q = '\0';
105     if (quote && *p == quote)
106         p++;
107     *pp = p;
108 }
109
110 void ffserver_parse_acl_row(FFServerStream *stream, FFServerStream* feed,
111                             FFServerIPAddressACL *ext_acl,
112                             const char *p, const char *filename, int line_num)
113 {
114     char arg[1024];
115     FFServerIPAddressACL acl;
116     int errors = 0;
117
118     ffserver_get_arg(arg, sizeof(arg), &p);
119     if (av_strcasecmp(arg, "allow") == 0)
120         acl.action = IP_ALLOW;
121     else if (av_strcasecmp(arg, "deny") == 0)
122         acl.action = IP_DENY;
123     else {
124         fprintf(stderr, "%s:%d: ACL action '%s' is not ALLOW or DENY\n",
125                 filename, line_num, arg);
126         errors++;
127     }
128
129     ffserver_get_arg(arg, sizeof(arg), &p);
130
131     if (resolve_host(&acl.first, arg)) {
132         fprintf(stderr, "%s:%d: ACL refers to invalid host or IP address '%s'\n",
133                 filename, line_num, arg);
134         errors++;
135     } else
136         acl.last = acl.first;
137
138     ffserver_get_arg(arg, sizeof(arg), &p);
139
140     if (arg[0]) {
141         if (resolve_host(&acl.last, arg)) {
142             fprintf(stderr,
143                     "%s:%d: ACL refers to invalid host or IP address '%s'\n",
144                     filename, line_num, arg);
145             errors++;
146         }
147     }
148
149     if (!errors) {
150         FFServerIPAddressACL *nacl = av_mallocz(sizeof(*nacl));
151         FFServerIPAddressACL **naclp = 0;
152
153         acl.next = 0;
154         *nacl = acl;
155
156         if (stream)
157             naclp = &stream->acl;
158         else if (feed)
159             naclp = &feed->acl;
160         else if (ext_acl)
161             naclp = &ext_acl;
162         else {
163             fprintf(stderr, "%s:%d: ACL found not in <stream> or <feed>\n",
164                     filename, line_num);
165             errors++;
166         }
167
168         if (naclp) {
169             while (*naclp)
170                 naclp = &(*naclp)->next;
171
172             *naclp = nacl;
173         } else
174             av_free(nacl);
175     }
176 }
177
178 /* add a codec and set the default parameters */
179 static void add_codec(FFServerStream *stream, AVCodecContext *av,
180                       FFServerConfig *config)
181 {
182     AVStream *st;
183     AVDictionary **opts, *recommended = NULL;
184     char *enc_config;
185
186     if(stream->nb_streams >= FF_ARRAY_ELEMS(stream->streams))
187         return;
188
189     opts = av->codec_type == AVMEDIA_TYPE_AUDIO ?
190            &config->audio_opts : &config->video_opts;
191     av_dict_copy(&recommended, *opts, 0);
192     av_opt_set_dict2(av->priv_data, opts, AV_OPT_SEARCH_CHILDREN);
193     av_opt_set_dict2(av, opts, AV_OPT_SEARCH_CHILDREN);
194     if (av_dict_count(*opts))
195         av_log(NULL, AV_LOG_WARNING,
196                "Something is wrong, %d options are not set!\n", av_dict_count(*opts));
197
198     if (config->stream_use_defaults) {
199     //TODO: reident
200     /* compute default parameters */
201     switch(av->codec_type) {
202     case AVMEDIA_TYPE_AUDIO:
203         if (av->bit_rate == 0) {
204             av->bit_rate = 64000;
205             av_dict_set_int(&recommended, "ab", av->bit_rate, 0);
206         }
207         if (av->sample_rate == 0) {
208             av->sample_rate = 22050;
209             av_dict_set_int(&recommended, "ar", av->sample_rate, 0);
210         }
211         if (av->channels == 0) {
212             av->channels = 1;
213             av_dict_set_int(&recommended, "ac", av->channels, 0);
214         }
215         break;
216     case AVMEDIA_TYPE_VIDEO:
217         if (av->bit_rate == 0) {
218             av->bit_rate = 64000;
219             av_dict_set_int(&recommended, "b", av->bit_rate, 0);
220         }
221         if (av->time_base.num == 0){
222             av->time_base.den = 5;
223             av->time_base.num = 1;
224             av_dict_set(&recommended, "time_base", "1/5", 0);
225         }
226         if (av->width == 0 || av->height == 0) {
227             av->width = 160;
228             av->height = 128;
229             av_dict_set(&recommended, "video_size", "160x128", 0);
230         }
231         /* Bitrate tolerance is less for streaming */
232         if (av->bit_rate_tolerance == 0) {
233             av->bit_rate_tolerance = FFMAX(av->bit_rate / 4,
234                       (int64_t)av->bit_rate*av->time_base.num/av->time_base.den);
235             av_dict_set_int(&recommended, "bt", av->bit_rate_tolerance, 0);
236         }
237         if (av->qmin == 0) {
238             av->qmin = 3;
239             av_dict_set_int(&recommended, "qmin", av->qmin, 0);
240         }
241         if (av->qmax == 0) {
242             av->qmax = 31;
243             av_dict_set_int(&recommended, "qmax", av->qmax, 0);
244         }
245         if (av->max_qdiff == 0) {
246             av->max_qdiff = 3;
247             av_dict_set_int(&recommended, "qdiff", av->max_qdiff, 0);
248         }
249         /*FIXME: 0.5 is a default for these two, it is a dead code */
250         av->qcompress = 0.5;
251         av_dict_set(&recommended, "qcomp", "0.5", 0);
252         av->qblur = 0.5;
253         av_dict_set(&recommended, "qblur", "0.5", 0);
254
255         if (!av->nsse_weight) {
256             av->nsse_weight = 8;
257             av_dict_set_int(&recommended, "nssew", av->nsse_weight, 0);
258         }
259
260         av->frame_skip_cmp = FF_CMP_DCTMAX;
261         av_dict_set_int(&recommended, "skipcmp", FF_CMP_DCTMAX, 0);
262         if (!av->me_method) {
263             av->me_method = ME_EPZS;
264             av_dict_set_int(&recommended, "me_method", ME_EPZS, 0);
265         }
266
267         /* FIXME: rc_buffer_aggressivity and rc_eq are deprecated */
268         av->rc_buffer_aggressivity = 1.0;
269         av_dict_set(&recommended, "rc_buf_aggressivity", "1.0", 0);
270
271         if (!av->rc_eq) {
272             av->rc_eq = av_strdup("tex^qComp");
273             av_dict_set(&recommended, "rc_eq", "tex^qComp", 0);
274         }
275         if (!av->i_quant_factor) {
276             av->i_quant_factor = -0.8;
277             av_dict_set(&recommended, "i_qfactor", "-0.8", 0);
278         }
279         if (!av->b_quant_factor) {
280             av->b_quant_factor = 1.25;
281             av_dict_set(&recommended, "b_qfactor", "1.25", 0);
282         }
283         if (!av->b_quant_offset) {
284             av->b_quant_offset = 1.25;
285             av_dict_set(&recommended, "b_qoffset", "1.25", 0);
286         }
287         if (!av->rc_max_rate) {
288             av->rc_max_rate = av->bit_rate * 2;
289             av_dict_set_int(&recommended, "maxrate", av->rc_max_rate, 0);
290         }
291
292         if (av->rc_max_rate && !av->rc_buffer_size) {
293             av->rc_buffer_size = av->rc_max_rate;
294             av_dict_set_int(&recommended, "bufsize", av->rc_buffer_size, 0);
295         }
296         break;
297     default:
298         abort();
299     }
300     } else {
301         switch(av->codec_type) {
302         case AVMEDIA_TYPE_AUDIO:
303             if (av->bit_rate == 0)
304                 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
305                                     &config->errors, "audio bit rate is not set\n");
306             if (av->sample_rate == 0)
307                 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
308                                     &config->errors, "audio sample rate is not set\n");
309             break;
310         case AVMEDIA_TYPE_VIDEO:
311             if (av->width == 0 || av->height == 0)
312                 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
313                                     &config->errors, "video size is not set\n");
314             break;
315         default:
316             av_assert0(0);
317         }
318     }
319
320     st = av_mallocz(sizeof(AVStream));
321     if (!st)
322         return;
323     av_dict_get_string(recommended, &enc_config, '=', ',');
324     av_dict_free(&recommended);
325     av_stream_set_recommended_encoder_configuration(st, enc_config);
326     st->codec = av;
327     stream->streams[stream->nb_streams++] = st;
328 }
329
330 static int ffserver_set_codec(AVCodecContext *ctx, const char *codec_name, FFServerConfig *config)
331 {
332     int ret;
333     AVCodec *codec = avcodec_find_encoder_by_name(codec_name);
334     if (!codec || codec->type != ctx->codec_type) {
335         report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
336                             &config->errors, "Invalid codec name: %s\n", codec_name);
337         return 0;
338     }
339     if (ctx->codec_id == AV_CODEC_ID_NONE && !ctx->priv_data) {
340         if ((ret = avcodec_get_context_defaults3(ctx, codec)) < 0)
341             return ret;
342         ctx->codec = codec;
343     }
344     if (ctx->codec_id != codec->id)
345         report_config_error(config->filename, config->line_num, AV_LOG_ERROR, &config->errors,
346                             "Inconsistent configuration: trying to set %s codec option, but %s codec is used previously\n",
347                             codec_name, avcodec_get_name(ctx->codec_id));
348     return 0;
349 }
350
351 static int ffserver_opt_preset(const char *arg, int type, FFServerConfig *config)
352 {
353     FILE *f=NULL;
354     char filename[1000], tmp[1000], tmp2[1000], line[1000];
355     int ret = 0;
356     AVCodecContext *avctx;
357     const AVCodec *codec;
358
359     switch(type) {
360     case AV_OPT_FLAG_AUDIO_PARAM:
361         avctx = config->dummy_actx;
362         break;
363     case AV_OPT_FLAG_VIDEO_PARAM:
364         avctx = config->dummy_vctx;
365         break;
366     default:
367         av_assert0(0);
368     }
369     codec = avcodec_find_encoder(avctx->codec_id);
370
371     if (!(f = get_preset_file(filename, sizeof(filename), arg, 0,
372                               codec ? codec->name : NULL))) {
373         av_log(NULL, AV_LOG_ERROR, "File for preset '%s' not found\n", arg);
374         return AVERROR(EINVAL);
375     }
376
377     while(!feof(f)){
378         int e= fscanf(f, "%999[^\n]\n", line) - 1;
379         if(line[0] == '#' && !e)
380             continue;
381         e|= sscanf(line, "%999[^=]=%999[^\n]\n", tmp, tmp2) - 2;
382         if(e){
383             av_log(NULL, AV_LOG_ERROR, "%s: Invalid syntax: '%s'\n", filename, line);
384             ret = AVERROR(EINVAL);
385             break;
386         }
387         if ((!strcmp(tmp, "acodec") && avctx->codec_type == AVMEDIA_TYPE_AUDIO) ||
388              !strcmp(tmp, "vcodec") && avctx->codec_type == AVMEDIA_TYPE_VIDEO)
389         {
390             if (ffserver_set_codec(avctx, tmp2, config) < 0)
391                 break;
392         } else if (!strcmp(tmp, "scodec")) {
393             av_log(NULL, AV_LOG_ERROR, "Subtitles preset found.\n");
394             ret = AVERROR(EINVAL);
395             break;
396         } else if (ffserver_save_avoption(tmp, tmp2, type, config) < 0)
397             break;
398     }
399
400     fclose(f);
401
402     return ret;
403 }
404
405 static AVOutputFormat *ffserver_guess_format(const char *short_name, const char *filename, const char *mime_type)
406 {
407     AVOutputFormat *fmt = av_guess_format(short_name, filename, mime_type);
408
409     if (fmt) {
410         AVOutputFormat *stream_fmt;
411         char stream_format_name[64];
412
413         snprintf(stream_format_name, sizeof(stream_format_name), "%s_stream",
414                 fmt->name);
415         stream_fmt = av_guess_format(stream_format_name, NULL, NULL);
416
417         if (stream_fmt)
418             fmt = stream_fmt;
419     }
420
421     return fmt;
422 }
423
424 static void vreport_config_error(const char *filename, int line_num, int log_level, int *errors, const char *fmt, va_list vl)
425 {
426     av_log(NULL, log_level, "%s:%d: ", filename, line_num);
427     av_vlog(NULL, log_level, fmt, vl);
428     if (errors)
429         (*errors)++;
430 }
431
432 static void report_config_error(const char *filename, int line_num, int log_level, int *errors, const char *fmt, ...)
433 {
434     va_list vl;
435     va_start(vl, fmt);
436     vreport_config_error(filename, line_num, log_level, errors, fmt, vl);
437     va_end(vl);
438 }
439
440 static int ffserver_set_int_param(int *dest, const char *value, int factor, int min, int max,
441                                   FFServerConfig *config, const char *error_msg, ...)
442 {
443     int tmp;
444     char *tailp;
445     if (!value || !value[0])
446         goto error;
447     errno = 0;
448     tmp = strtol(value, &tailp, 0);
449     if (tmp < min || tmp > max)
450         goto error;
451     if (factor) {
452         if (FFABS(tmp) > INT_MAX / FFABS(factor))
453             goto error;
454         tmp *= factor;
455     }
456     if (tailp[0] || errno)
457         goto error;
458     if (dest)
459         *dest = tmp;
460     return 0;
461   error:
462     if (config) {
463         va_list vl;
464         va_start(vl, error_msg);
465         vreport_config_error(config->filename, config->line_num, AV_LOG_ERROR,
466                 &config->errors, error_msg, vl);
467         va_end(vl);
468     }
469     return AVERROR(EINVAL);
470 }
471
472 static int ffserver_set_float_param(float *dest, const char *value, float factor, float min, float max,
473                                     FFServerConfig *config, const char *error_msg, ...)
474 {
475     double tmp;
476     char *tailp;
477     if (!value || !value[0])
478         goto error;
479     errno = 0;
480     tmp = strtod(value, &tailp);
481     if (tmp < min || tmp > max)
482         goto error;
483     if (factor)
484         tmp *= factor;
485     if (tailp[0] || errno)
486         goto error;
487     if (dest)
488         *dest = tmp;
489     return 0;
490   error:
491     if (config) {
492         va_list vl;
493         va_start(vl, error_msg);
494         vreport_config_error(config->filename, config->line_num, AV_LOG_ERROR,
495                 &config->errors, error_msg, vl);
496         va_end(vl);
497     }
498     return AVERROR(EINVAL);
499 }
500
501 static int ffserver_save_avoption(const char *opt, const char *arg, int type, FFServerConfig *config)
502 {
503     static int hinted = 0;
504     int ret = 0;
505     AVDictionaryEntry *e;
506     const AVOption *o = NULL;
507     const char *option = NULL;
508     const char *codec_name = NULL;
509     char buff[1024];
510     AVCodecContext *ctx;
511     AVDictionary **dict;
512     enum AVCodecID guessed_codec_id;
513
514     switch (type) {
515     case AV_OPT_FLAG_VIDEO_PARAM:
516         ctx = config->dummy_vctx;
517         dict = &config->video_opts;
518         guessed_codec_id = config->guessed_video_codec_id != AV_CODEC_ID_NONE ?
519                            config->guessed_video_codec_id : AV_CODEC_ID_H264;
520         break;
521     case AV_OPT_FLAG_AUDIO_PARAM:
522         ctx = config->dummy_actx;
523         dict = &config->audio_opts;
524         guessed_codec_id = config->guessed_audio_codec_id != AV_CODEC_ID_NONE ?
525                            config->guessed_audio_codec_id : AV_CODEC_ID_AAC;
526         break;
527     default:
528         av_assert0(0);
529     }
530
531     if (strchr(opt, ':')) {
532         //explicit private option
533         snprintf(buff, sizeof(buff), "%s", opt);
534         codec_name = buff;
535         option = strchr(buff, ':');
536         buff[option - buff] = '\0';
537         option++;
538         if ((ret = ffserver_set_codec(ctx, codec_name, config)) < 0)
539             return ret;
540         if (!ctx->codec || !ctx->priv_data)
541             return -1;
542     } else {
543         option = opt;
544     }
545
546     o = av_opt_find(ctx, option, NULL, type | AV_OPT_FLAG_ENCODING_PARAM, AV_OPT_SEARCH_CHILDREN);
547     if (!o && (!strcmp(option, "time_base")  || !strcmp(option, "pixel_format") ||
548                !strcmp(option, "video_size") || !strcmp(option, "codec_tag")))
549         o = av_opt_find(ctx, option, NULL, 0, 0);
550     if (!o) {
551         report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
552                             &config->errors, "Option not found: %s\n", opt);
553         if (!hinted && ctx->codec_id == AV_CODEC_ID_NONE) {
554             hinted = 1;
555             report_config_error(config->filename, config->line_num, AV_LOG_ERROR, NULL,
556                                 "If '%s' is a codec private option, then prefix it with codec name, "
557                                 "for example '%s:%s %s' or define codec earlier.\n",
558                                 opt, avcodec_get_name(guessed_codec_id) ,opt, arg);
559         }
560     } else if ((ret = av_opt_set(ctx, option, arg, AV_OPT_SEARCH_CHILDREN)) < 0) {
561         report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
562                 &config->errors, "Invalid value for option %s (%s): %s\n", opt,
563                 arg, av_err2str(ret));
564     } else if ((e = av_dict_get(*dict, option, NULL, 0))) {
565         if ((o->type == AV_OPT_TYPE_FLAGS) && arg && (arg[0] == '+' || arg[0] == '-'))
566             return av_dict_set(dict, option, arg, AV_DICT_APPEND);
567         report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
568                 &config->errors,
569                 "Redeclaring value of the option %s, previous value: %s\n",
570                 opt, e->value);
571     } else if (av_dict_set(dict, option, arg, 0) < 0) {
572         return AVERROR(ENOMEM);
573     }
574     return 0;
575 }
576
577 static int ffserver_save_avoption_int(const char *opt, int64_t arg,
578                                       int type, FFServerConfig *config)
579 {
580     char buf[22];
581     snprintf(buf, sizeof(buf), "%"PRId64, arg);
582     return ffserver_save_avoption(opt, buf, type, config);
583 }
584
585 #define ERROR(...)   report_config_error(config->filename, config->line_num, AV_LOG_ERROR,   &config->errors,   __VA_ARGS__)
586 #define WARNING(...) report_config_error(config->filename, config->line_num, AV_LOG_WARNING, &config->warnings, __VA_ARGS__)
587
588 static int ffserver_parse_config_global(FFServerConfig *config, const char *cmd,
589                                         const char **p)
590 {
591     int val;
592     char arg[1024];
593     if (!av_strcasecmp(cmd, "Port") || !av_strcasecmp(cmd, "HTTPPort")) {
594         if (!av_strcasecmp(cmd, "Port"))
595             WARNING("Port option is deprecated, use HTTPPort instead\n");
596         ffserver_get_arg(arg, sizeof(arg), p);
597         ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
598                 "Invalid port: %s\n", arg);
599         if (val < 1024)
600             WARNING("Trying to use IETF assigned system port: %d\n", val);
601         config->http_addr.sin_port = htons(val);
602     } else if (!av_strcasecmp(cmd, "HTTPBindAddress") || !av_strcasecmp(cmd, "BindAddress")) {
603         if (!av_strcasecmp(cmd, "BindAddress"))
604             WARNING("BindAddress option is deprecated, use HTTPBindAddress instead\n");
605         ffserver_get_arg(arg, sizeof(arg), p);
606         if (resolve_host(&config->http_addr.sin_addr, arg))
607             ERROR("Invalid host/IP address: %s\n", arg);
608     } else if (!av_strcasecmp(cmd, "NoDaemon")) {
609         WARNING("NoDaemon option has no effect, you should remove it\n");
610     } else if (!av_strcasecmp(cmd, "RTSPPort")) {
611         ffserver_get_arg(arg, sizeof(arg), p);
612         ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
613                 "Invalid port: %s\n", arg);
614         config->rtsp_addr.sin_port = htons(val);
615     } else if (!av_strcasecmp(cmd, "RTSPBindAddress")) {
616         ffserver_get_arg(arg, sizeof(arg), p);
617         if (resolve_host(&config->rtsp_addr.sin_addr, arg))
618             ERROR("Invalid host/IP address: %s\n", arg);
619     } else if (!av_strcasecmp(cmd, "MaxHTTPConnections")) {
620         ffserver_get_arg(arg, sizeof(arg), p);
621         ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
622                 "Invalid MaxHTTPConnections: %s\n", arg);
623         config->nb_max_http_connections = val;
624         if (config->nb_max_connections > config->nb_max_http_connections)
625             ERROR("Inconsistent configuration: MaxClients(%d) > MaxHTTPConnections(%d)\n",
626                   config->nb_max_connections, config->nb_max_http_connections);
627     } else if (!av_strcasecmp(cmd, "MaxClients")) {
628         ffserver_get_arg(arg, sizeof(arg), p);
629         ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
630                 "Invalid MaxClients: %s\n", arg);
631         config->nb_max_connections = val;
632         if (config->nb_max_connections > config->nb_max_http_connections)
633             ERROR("Inconsistent configuration: MaxClients(%d) > MaxHTTPConnections(%d)\n",
634                   config->nb_max_connections, config->nb_max_http_connections);
635     } else if (!av_strcasecmp(cmd, "MaxBandwidth")) {
636         int64_t llval;
637         char *tailp;
638         ffserver_get_arg(arg, sizeof(arg), p);
639         errno = 0;
640         llval = strtoll(arg, &tailp, 10);
641         if (llval < 10 || llval > 10000000 || tailp[0] || errno)
642             ERROR("Invalid MaxBandwidth: %s\n", arg);
643         else
644             config->max_bandwidth = llval;
645     } else if (!av_strcasecmp(cmd, "CustomLog")) {
646         if (!config->debug)
647             ffserver_get_arg(config->logfilename, sizeof(config->logfilename), p);
648     } else if (!av_strcasecmp(cmd, "LoadModule")) {
649         ERROR("Loadable modules are no longer supported\n");
650     } else if (!av_strcasecmp(cmd, "NoDefaults")) {
651         config->use_defaults = 0;
652     } else if (!av_strcasecmp(cmd, "UseDefaults")) {
653         config->use_defaults = 1;
654     } else
655         ERROR("Incorrect keyword: '%s'\n", cmd);
656     return 0;
657 }
658
659 static int ffserver_parse_config_feed(FFServerConfig *config, const char *cmd, const char **p,
660                                       FFServerStream **pfeed)
661 {
662     FFServerStream *feed;
663     char arg[1024];
664     av_assert0(pfeed);
665     feed = *pfeed;
666     if (!av_strcasecmp(cmd, "<Feed")) {
667         char *q;
668         FFServerStream *s;
669         feed = av_mallocz(sizeof(FFServerStream));
670         if (!feed)
671             return AVERROR(ENOMEM);
672         ffserver_get_arg(feed->filename, sizeof(feed->filename), p);
673         q = strrchr(feed->filename, '>');
674         if (*q)
675             *q = '\0';
676
677         for (s = config->first_feed; s; s = s->next) {
678             if (!strcmp(feed->filename, s->filename))
679                 ERROR("Feed '%s' already registered\n", s->filename);
680         }
681
682         feed->fmt = av_guess_format("ffm", NULL, NULL);
683         /* default feed file */
684         snprintf(feed->feed_filename, sizeof(feed->feed_filename),
685                  "/tmp/%s.ffm", feed->filename);
686         feed->feed_max_size = 5 * 1024 * 1024;
687         feed->is_feed = 1;
688         feed->feed = feed; /* self feeding :-) */
689         *pfeed = feed;
690         return 0;
691     }
692     av_assert0(feed);
693     if (!av_strcasecmp(cmd, "Launch")) {
694         int i;
695
696         feed->child_argv = av_mallocz_array(MAX_CHILD_ARGS, sizeof(char *));
697         if (!feed->child_argv)
698             return AVERROR(ENOMEM);
699         for (i = 0; i < MAX_CHILD_ARGS - 2; i++) {
700             ffserver_get_arg(arg, sizeof(arg), p);
701             if (!arg[0])
702                 break;
703
704             feed->child_argv[i] = av_strdup(arg);
705             if (!feed->child_argv[i])
706                 return AVERROR(ENOMEM);
707         }
708
709         feed->child_argv[i] =
710             av_asprintf("http://%s:%d/%s",
711                         (config->http_addr.sin_addr.s_addr == INADDR_ANY) ? "127.0.0.1" :
712                         inet_ntoa(config->http_addr.sin_addr), ntohs(config->http_addr.sin_port),
713                         feed->filename);
714         if (!feed->child_argv[i])
715             return AVERROR(ENOMEM);
716     } else if (!av_strcasecmp(cmd, "ACL")) {
717         ffserver_parse_acl_row(NULL, feed, NULL, *p, config->filename,
718                 config->line_num);
719     } else if (!av_strcasecmp(cmd, "File") || !av_strcasecmp(cmd, "ReadOnlyFile")) {
720         ffserver_get_arg(feed->feed_filename, sizeof(feed->feed_filename), p);
721         feed->readonly = !av_strcasecmp(cmd, "ReadOnlyFile");
722     } else if (!av_strcasecmp(cmd, "Truncate")) {
723         ffserver_get_arg(arg, sizeof(arg), p);
724         /* assume Truncate is true in case no argument is specified */
725         if (!arg[0]) {
726             feed->truncate = 1;
727         } else {
728             WARNING("Truncate N syntax in configuration file is deprecated, "
729                     "use Truncate alone with no arguments\n");
730             feed->truncate = strtod(arg, NULL);
731         }
732     } else if (!av_strcasecmp(cmd, "FileMaxSize")) {
733         char *p1;
734         double fsize;
735
736         ffserver_get_arg(arg, sizeof(arg), p);
737         p1 = arg;
738         fsize = strtod(p1, &p1);
739         switch(av_toupper(*p1)) {
740         case 'K':
741             fsize *= 1024;
742             break;
743         case 'M':
744             fsize *= 1024 * 1024;
745             break;
746         case 'G':
747             fsize *= 1024 * 1024 * 1024;
748             break;
749         default:
750             ERROR("Invalid file size: %s\n", arg);
751             break;
752         }
753         feed->feed_max_size = (int64_t)fsize;
754         if (feed->feed_max_size < FFM_PACKET_SIZE*4)
755             ERROR("Feed max file size is too small, must be at least %d\n",
756                     FFM_PACKET_SIZE*4);
757     } else if (!av_strcasecmp(cmd, "</Feed>")) {
758         *pfeed = NULL;
759     } else {
760         ERROR("Invalid entry '%s' inside <Feed></Feed>\n", cmd);
761     }
762     return 0;
763 }
764
765 static int ffserver_parse_config_stream(FFServerConfig *config, const char *cmd, const char **p,
766                                         FFServerStream **pstream)
767 {
768     char arg[1024], arg2[1024];
769     FFServerStream *stream;
770     int val;
771
772     av_assert0(pstream);
773     stream = *pstream;
774
775     if (!av_strcasecmp(cmd, "<Stream")) {
776         char *q;
777         FFServerStream *s;
778         stream = av_mallocz(sizeof(FFServerStream));
779         if (!stream)
780             return AVERROR(ENOMEM);
781         config->dummy_actx = avcodec_alloc_context3(NULL);
782         config->dummy_vctx = avcodec_alloc_context3(NULL);
783         if (!config->dummy_vctx || !config->dummy_actx) {
784             av_free(stream);
785             avcodec_free_context(&config->dummy_vctx);
786             avcodec_free_context(&config->dummy_actx);
787             return AVERROR(ENOMEM);
788         }
789         config->dummy_actx->codec_type = AVMEDIA_TYPE_AUDIO;
790         config->dummy_vctx->codec_type = AVMEDIA_TYPE_VIDEO;
791         ffserver_get_arg(stream->filename, sizeof(stream->filename), p);
792         q = strrchr(stream->filename, '>');
793         if (q)
794             *q = '\0';
795
796         for (s = config->first_stream; s; s = s->next) {
797             if (!strcmp(stream->filename, s->filename))
798                 ERROR("Stream '%s' already registered\n", s->filename);
799         }
800
801         stream->fmt = ffserver_guess_format(NULL, stream->filename, NULL);
802         if (stream->fmt) {
803             config->guessed_audio_codec_id = stream->fmt->audio_codec;
804             config->guessed_video_codec_id = stream->fmt->video_codec;
805         } else {
806             config->guessed_audio_codec_id = AV_CODEC_ID_NONE;
807             config->guessed_video_codec_id = AV_CODEC_ID_NONE;
808         }
809         config->stream_use_defaults = config->use_defaults;
810         *pstream = stream;
811         return 0;
812     }
813     av_assert0(stream);
814     if (!av_strcasecmp(cmd, "Feed")) {
815         FFServerStream *sfeed;
816         ffserver_get_arg(arg, sizeof(arg), p);
817         sfeed = config->first_feed;
818         while (sfeed) {
819             if (!strcmp(sfeed->filename, arg))
820                 break;
821             sfeed = sfeed->next_feed;
822         }
823         if (!sfeed)
824             ERROR("Feed with name '%s' for stream '%s' is not defined\n", arg,
825                     stream->filename);
826         else
827             stream->feed = sfeed;
828     } else if (!av_strcasecmp(cmd, "Format")) {
829         ffserver_get_arg(arg, sizeof(arg), p);
830         if (!strcmp(arg, "status")) {
831             stream->stream_type = STREAM_TYPE_STATUS;
832             stream->fmt = NULL;
833         } else {
834             stream->stream_type = STREAM_TYPE_LIVE;
835             /* JPEG cannot be used here, so use single frame MJPEG */
836             if (!strcmp(arg, "jpeg"))
837                 strcpy(arg, "mjpeg");
838             stream->fmt = ffserver_guess_format(arg, NULL, NULL);
839             if (!stream->fmt)
840                 ERROR("Unknown Format: %s\n", arg);
841         }
842         if (stream->fmt) {
843             config->guessed_audio_codec_id = stream->fmt->audio_codec;
844             config->guessed_video_codec_id = stream->fmt->video_codec;
845         }
846     } else if (!av_strcasecmp(cmd, "InputFormat")) {
847         ffserver_get_arg(arg, sizeof(arg), p);
848         stream->ifmt = av_find_input_format(arg);
849         if (!stream->ifmt)
850             ERROR("Unknown input format: %s\n", arg);
851     } else if (!av_strcasecmp(cmd, "FaviconURL")) {
852         if (stream->stream_type == STREAM_TYPE_STATUS)
853             ffserver_get_arg(stream->feed_filename,
854                     sizeof(stream->feed_filename), p);
855         else
856             ERROR("FaviconURL only permitted for status streams\n");
857     } else if (!av_strcasecmp(cmd, "Author")    ||
858                !av_strcasecmp(cmd, "Comment")   ||
859                !av_strcasecmp(cmd, "Copyright") ||
860                !av_strcasecmp(cmd, "Title")) {
861         char key[32];
862         int i;
863         ffserver_get_arg(arg, sizeof(arg), p);
864         for (i = 0; i < strlen(cmd); i++)
865             key[i] = av_tolower(cmd[i]);
866         key[i] = 0;
867         WARNING("'%s' option in configuration file is deprecated, "
868                 "use 'Metadata %s VALUE' instead\n", cmd, key);
869         if (av_dict_set(&stream->metadata, key, arg, 0) < 0)
870             goto nomem;
871     } else if (!av_strcasecmp(cmd, "Metadata")) {
872         ffserver_get_arg(arg, sizeof(arg), p);
873         ffserver_get_arg(arg2, sizeof(arg2), p);
874         if (av_dict_set(&stream->metadata, arg, arg2, 0) < 0)
875             goto nomem;
876     } else if (!av_strcasecmp(cmd, "Preroll")) {
877         ffserver_get_arg(arg, sizeof(arg), p);
878         stream->prebuffer = atof(arg) * 1000;
879     } else if (!av_strcasecmp(cmd, "StartSendOnKey")) {
880         stream->send_on_key = 1;
881     } else if (!av_strcasecmp(cmd, "AudioCodec")) {
882         ffserver_get_arg(arg, sizeof(arg), p);
883         ffserver_set_codec(config->dummy_actx, arg, config);
884     } else if (!av_strcasecmp(cmd, "VideoCodec")) {
885         ffserver_get_arg(arg, sizeof(arg), p);
886         ffserver_set_codec(config->dummy_vctx, arg, config);
887     } else if (!av_strcasecmp(cmd, "MaxTime")) {
888         ffserver_get_arg(arg, sizeof(arg), p);
889         stream->max_time = atof(arg) * 1000;
890     } else if (!av_strcasecmp(cmd, "AudioBitRate")) {
891         float f;
892         ffserver_get_arg(arg, sizeof(arg), p);
893         ffserver_set_float_param(&f, arg, 1000, -FLT_MAX, FLT_MAX, config,
894                 "Invalid %s: %s\n", cmd, arg);
895         if (ffserver_save_avoption_int("ab", (int64_t)lrintf(f), AV_OPT_FLAG_AUDIO_PARAM, config) < 0)
896             goto nomem;
897     } else if (!av_strcasecmp(cmd, "AudioChannels")) {
898         ffserver_get_arg(arg, sizeof(arg), p);
899         if (ffserver_save_avoption("ac", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0)
900             goto nomem;
901     } else if (!av_strcasecmp(cmd, "AudioSampleRate")) {
902         ffserver_get_arg(arg, sizeof(arg), p);
903         if (ffserver_save_avoption("ar", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0)
904             goto nomem;
905     } else if (!av_strcasecmp(cmd, "VideoBitRateRange")) {
906         int minrate, maxrate;
907         char *dash;
908         ffserver_get_arg(arg, sizeof(arg), p);
909         dash = strchr(arg, '-');
910         if (dash) {
911             *dash = '\0';
912             dash++;
913             if (ffserver_set_int_param(&minrate, arg,  1000, 0, INT_MAX, config, "Invalid %s: %s", cmd, arg) >= 0 &&
914                 ffserver_set_int_param(&maxrate, dash, 1000, 0, INT_MAX, config, "Invalid %s: %s", cmd, arg) >= 0) {
915                 if (ffserver_save_avoption_int("minrate", minrate, AV_OPT_FLAG_VIDEO_PARAM, config) < 0 ||
916                     ffserver_save_avoption_int("maxrate", maxrate, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
917                 goto nomem;
918             }
919         } else
920             ERROR("Incorrect format for VideoBitRateRange -- should be "
921                     "<min>-<max>: %s\n", arg);
922     } else if (!av_strcasecmp(cmd, "Debug")) {
923         ffserver_get_arg(arg, sizeof(arg), p);
924         if (ffserver_save_avoption("debug", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0 ||
925             ffserver_save_avoption("debug", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
926             goto nomem;
927     } else if (!av_strcasecmp(cmd, "Strict")) {
928         ffserver_get_arg(arg, sizeof(arg), p);
929         if (ffserver_save_avoption("strict", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0 ||
930             ffserver_save_avoption("strict", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
931             goto nomem;
932     } else if (!av_strcasecmp(cmd, "VideoBufferSize")) {
933         ffserver_get_arg(arg, sizeof(arg), p);
934         ffserver_set_int_param(&val, arg, 8*1024, 0, INT_MAX, config,
935                 "Invalid %s: %s", cmd, arg);
936         if (ffserver_save_avoption_int("bufsize", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
937             goto nomem;
938     } else if (!av_strcasecmp(cmd, "VideoBitRateTolerance")) {
939         ffserver_get_arg(arg, sizeof(arg), p);
940         ffserver_set_int_param(&val, arg, 1000, INT_MIN, INT_MAX, config,
941                 "Invalid %s: %s", cmd, arg);
942         if (ffserver_save_avoption_int("bt", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
943             goto nomem;
944     } else if (!av_strcasecmp(cmd, "VideoBitRate")) {
945         ffserver_get_arg(arg, sizeof(arg), p);
946         ffserver_set_int_param(&val, arg, 1000, INT_MIN, INT_MAX, config,
947                 "Invalid %s: %s", cmd, arg);
948         if (ffserver_save_avoption_int("b", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
949            goto nomem;
950     } else if (!av_strcasecmp(cmd, "VideoSize")) {
951         int ret, w, h;
952         ffserver_get_arg(arg, sizeof(arg), p);
953         ret = av_parse_video_size(&w, &h, arg);
954         if (ret < 0)
955             ERROR("Invalid video size '%s'\n", arg);
956         else {
957             if (w % 2 || h % 2)
958                 WARNING("Image size is not a multiple of 2\n");
959             if (ffserver_save_avoption("video_size", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
960                 goto nomem;
961         }
962     } else if (!av_strcasecmp(cmd, "VideoFrameRate")) {
963         ffserver_get_arg(&arg[2], sizeof(arg) - 2, p);
964         arg[0] = '1'; arg[1] = '/';
965         if (ffserver_save_avoption("time_base", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
966             goto nomem;
967     } else if (!av_strcasecmp(cmd, "PixelFormat")) {
968         enum AVPixelFormat pix_fmt;
969         ffserver_get_arg(arg, sizeof(arg), p);
970         pix_fmt = av_get_pix_fmt(arg);
971         if (pix_fmt == AV_PIX_FMT_NONE)
972             ERROR("Unknown pixel format: %s\n", arg);
973         else if (ffserver_save_avoption("pixel_format", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
974             goto nomem;
975     } else if (!av_strcasecmp(cmd, "VideoGopSize")) {
976         ffserver_get_arg(arg, sizeof(arg), p);
977         if (ffserver_save_avoption("g", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
978             goto nomem;
979     } else if (!av_strcasecmp(cmd, "VideoIntraOnly")) {
980         if (ffserver_save_avoption("g", "1", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
981             goto nomem;
982     } else if (!av_strcasecmp(cmd, "VideoHighQuality")) {
983         if (ffserver_save_avoption("mbd", "+bits", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
984             goto nomem;
985     } else if (!av_strcasecmp(cmd, "Video4MotionVector")) {
986         if (ffserver_save_avoption("mbd", "+bits",  AV_OPT_FLAG_VIDEO_PARAM, config) < 0 || //FIXME remove
987             ffserver_save_avoption("flags", "+mv4", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
988             goto nomem;
989     } else if (!av_strcasecmp(cmd, "AVOptionVideo") ||
990                !av_strcasecmp(cmd, "AVOptionAudio")) {
991         int ret;
992         ffserver_get_arg(arg, sizeof(arg), p);
993         ffserver_get_arg(arg2, sizeof(arg2), p);
994         if (!av_strcasecmp(cmd, "AVOptionVideo"))
995             ret = ffserver_save_avoption(arg, arg2, AV_OPT_FLAG_VIDEO_PARAM, config);
996         else
997             ret = ffserver_save_avoption(arg, arg2, AV_OPT_FLAG_AUDIO_PARAM, config);
998         if (ret < 0)
999             goto nomem;
1000     } else if (!av_strcasecmp(cmd, "AVPresetVideo") ||
1001                !av_strcasecmp(cmd, "AVPresetAudio")) {
1002         ffserver_get_arg(arg, sizeof(arg), p);
1003         if (!av_strcasecmp(cmd, "AVPresetVideo"))
1004             ffserver_opt_preset(arg, AV_OPT_FLAG_VIDEO_PARAM, config);
1005         else
1006             ffserver_opt_preset(arg, AV_OPT_FLAG_AUDIO_PARAM, config);
1007     } else if (!av_strcasecmp(cmd, "VideoTag")) {
1008         ffserver_get_arg(arg, sizeof(arg), p);
1009         if (strlen(arg) == 4 &&
1010             ffserver_save_avoption_int("codec_tag", MKTAG(arg[0], arg[1], arg[2], arg[3]),
1011                                        AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1012             goto nomem;
1013     } else if (!av_strcasecmp(cmd, "BitExact")) {
1014         if (ffserver_save_avoption("flags", "+bitexact", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1015             goto nomem;
1016     } else if (!av_strcasecmp(cmd, "DctFastint")) {
1017         if (ffserver_save_avoption("dct", "fastint", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1018             goto nomem;
1019     } else if (!av_strcasecmp(cmd, "IdctSimple")) {
1020         if (ffserver_save_avoption("idct", "simple", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1021             goto nomem;
1022     } else if (!av_strcasecmp(cmd, "Qscale")) {
1023         ffserver_get_arg(arg, sizeof(arg), p);
1024         ffserver_set_int_param(&val, arg, 0, INT_MIN, INT_MAX, config,
1025                                "Invalid Qscale: %s\n", arg);
1026         if (ffserver_save_avoption("flags", "+qscale", AV_OPT_FLAG_VIDEO_PARAM, config) < 0 ||
1027             ffserver_save_avoption_int("global_quality", FF_QP2LAMBDA * val,
1028                                        AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1029             goto nomem;
1030     } else if (!av_strcasecmp(cmd, "VideoQDiff")) {
1031         ffserver_get_arg(arg, sizeof(arg), p);
1032         if (ffserver_save_avoption("qdiff", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1033             goto nomem;
1034     } else if (!av_strcasecmp(cmd, "VideoQMax")) {
1035         ffserver_get_arg(arg, sizeof(arg), p);
1036         if (ffserver_save_avoption("qmax", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1037             goto nomem;
1038     } else if (!av_strcasecmp(cmd, "VideoQMin")) {
1039         ffserver_get_arg(arg, sizeof(arg), p);
1040         if (ffserver_save_avoption("qmin", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1041             goto nomem;
1042     } else if (!av_strcasecmp(cmd, "LumiMask")) {
1043         ffserver_get_arg(arg, sizeof(arg), p);
1044         if (ffserver_save_avoption("lumi_mask", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1045             goto nomem;
1046     } else if (!av_strcasecmp(cmd, "DarkMask")) {
1047         ffserver_get_arg(arg, sizeof(arg), p);
1048         if (ffserver_save_avoption("dark_mask", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1049             goto nomem;
1050     } else if (!av_strcasecmp(cmd, "NoVideo")) {
1051         config->no_video = 1;
1052     } else if (!av_strcasecmp(cmd, "NoAudio")) {
1053         config->no_audio = 1;
1054     } else if (!av_strcasecmp(cmd, "ACL")) {
1055         ffserver_parse_acl_row(stream, NULL, NULL, *p, config->filename,
1056                 config->line_num);
1057     } else if (!av_strcasecmp(cmd, "DynamicACL")) {
1058         ffserver_get_arg(stream->dynamic_acl, sizeof(stream->dynamic_acl), p);
1059     } else if (!av_strcasecmp(cmd, "RTSPOption")) {
1060         ffserver_get_arg(arg, sizeof(arg), p);
1061         av_freep(&stream->rtsp_option);
1062         stream->rtsp_option = av_strdup(arg);
1063     } else if (!av_strcasecmp(cmd, "MulticastAddress")) {
1064         ffserver_get_arg(arg, sizeof(arg), p);
1065         if (resolve_host(&stream->multicast_ip, arg))
1066             ERROR("Invalid host/IP address: %s\n", arg);
1067         stream->is_multicast = 1;
1068         stream->loop = 1; /* default is looping */
1069     } else if (!av_strcasecmp(cmd, "MulticastPort")) {
1070         ffserver_get_arg(arg, sizeof(arg), p);
1071         ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
1072                 "Invalid MulticastPort: %s\n", arg);
1073         stream->multicast_port = val;
1074     } else if (!av_strcasecmp(cmd, "MulticastTTL")) {
1075         ffserver_get_arg(arg, sizeof(arg), p);
1076         ffserver_set_int_param(&val, arg, 0, INT_MIN, INT_MAX, config,
1077                 "Invalid MulticastTTL: %s\n", arg);
1078         stream->multicast_ttl = val;
1079     } else if (!av_strcasecmp(cmd, "NoLoop")) {
1080         stream->loop = 0;
1081     } else if (!av_strcasecmp(cmd, "</Stream>")) {
1082         config->stream_use_defaults &= 1;
1083         if (stream->feed && stream->fmt && strcmp(stream->fmt->name, "ffm")) {
1084             if (config->dummy_actx->codec_id == AV_CODEC_ID_NONE)
1085                 config->dummy_actx->codec_id = config->guessed_audio_codec_id;
1086             if (!config->no_audio && config->dummy_actx->codec_id != AV_CODEC_ID_NONE) {
1087                 AVCodecContext *audio_enc = avcodec_alloc_context3(avcodec_find_encoder(config->dummy_actx->codec_id));
1088                 add_codec(stream, audio_enc, config);
1089             }
1090             if (config->dummy_vctx->codec_id == AV_CODEC_ID_NONE)
1091                 config->dummy_vctx->codec_id = config->guessed_video_codec_id;
1092             if (!config->no_video && config->dummy_vctx->codec_id != AV_CODEC_ID_NONE) {
1093                 AVCodecContext *video_enc = avcodec_alloc_context3(avcodec_find_encoder(config->dummy_vctx->codec_id));
1094                 add_codec(stream, video_enc, config);
1095             }
1096         }
1097         av_dict_free(&config->video_opts);
1098         av_dict_free(&config->audio_opts);
1099         avcodec_free_context(&config->dummy_vctx);
1100         avcodec_free_context(&config->dummy_actx);
1101         *pstream = NULL;
1102     } else if (!av_strcasecmp(cmd, "File") || !av_strcasecmp(cmd, "ReadOnlyFile")) {
1103         ffserver_get_arg(stream->feed_filename, sizeof(stream->feed_filename),
1104                 p);
1105     } else if (!av_strcasecmp(cmd, "UseDefaults")) {
1106         if (config->stream_use_defaults > 1)
1107             WARNING("Multiple UseDefaults/NoDefaults entries.\n");
1108         config->stream_use_defaults = 3;
1109     } else if (!av_strcasecmp(cmd, "NoDefaults")) {
1110         if (config->stream_use_defaults > 1)
1111             WARNING("Multiple UseDefaults/NoDefaults entries.\n");
1112         config->stream_use_defaults = 2;
1113     } else {
1114         ERROR("Invalid entry '%s' inside <Stream></Stream>\n", cmd);
1115     }
1116     return 0;
1117   nomem:
1118     av_log(NULL, AV_LOG_ERROR, "Out of memory. Aborting.\n");
1119     av_dict_free(&config->video_opts);
1120     av_dict_free(&config->audio_opts);
1121     avcodec_free_context(&config->dummy_vctx);
1122     avcodec_free_context(&config->dummy_actx);
1123     return AVERROR(ENOMEM);
1124 }
1125
1126 static int ffserver_parse_config_redirect(FFServerConfig *config, const char *cmd, const char **p,
1127                                           FFServerStream **predirect)
1128 {
1129     FFServerStream *redirect;
1130     av_assert0(predirect);
1131     redirect = *predirect;
1132
1133     if (!av_strcasecmp(cmd, "<Redirect")) {
1134         char *q;
1135         redirect = av_mallocz(sizeof(FFServerStream));
1136         if (!redirect)
1137             return AVERROR(ENOMEM);
1138
1139         ffserver_get_arg(redirect->filename, sizeof(redirect->filename), p);
1140         q = strrchr(redirect->filename, '>');
1141         if (*q)
1142             *q = '\0';
1143         redirect->stream_type = STREAM_TYPE_REDIRECT;
1144         *predirect = redirect;
1145         return 0;
1146     }
1147     av_assert0(redirect);
1148     if (!av_strcasecmp(cmd, "URL")) {
1149         ffserver_get_arg(redirect->feed_filename,
1150                 sizeof(redirect->feed_filename), p);
1151     } else if (!av_strcasecmp(cmd, "</Redirect>")) {
1152         if (!redirect->feed_filename[0])
1153             ERROR("No URL found for <Redirect>\n");
1154         *predirect = NULL;
1155     } else {
1156         ERROR("Invalid entry '%s' inside <Redirect></Redirect>\n", cmd);
1157     }
1158     return 0;
1159 }
1160
1161 int ffserver_parse_ffconfig(const char *filename, FFServerConfig *config)
1162 {
1163     FILE *f;
1164     char line[1024];
1165     char cmd[64];
1166     const char *p;
1167     FFServerStream **last_stream, *stream = NULL, *redirect = NULL;
1168     FFServerStream **last_feed, *feed = NULL;
1169     int ret = 0;
1170
1171     av_assert0(config);
1172
1173     config->line_num = 0;
1174     f = fopen(filename, "r");
1175     if (!f) {
1176         ret = AVERROR(errno);
1177         av_log(NULL, AV_LOG_ERROR,
1178                 "Could not open the configuration file '%s'\n", filename);
1179         return ret;
1180     }
1181
1182     config->first_stream = NULL;
1183     last_stream = &config->first_stream;
1184     config->first_feed = NULL;
1185     last_feed = &config->first_feed;
1186     config->errors = config->warnings = 0;
1187
1188     for(;;) {
1189         if (fgets(line, sizeof(line), f) == NULL)
1190             break;
1191         config->line_num++;
1192         p = line;
1193         while (av_isspace(*p))
1194             p++;
1195         if (*p == '\0' || *p == '#')
1196             continue;
1197
1198         ffserver_get_arg(cmd, sizeof(cmd), &p);
1199
1200         if (feed || !av_strcasecmp(cmd, "<Feed")) {
1201             int opening = !av_strcasecmp(cmd, "<Feed");
1202             if (opening && (stream || feed || redirect)) {
1203                 ERROR("Already in a tag\n");
1204             } else {
1205                 if ((ret = ffserver_parse_config_feed(config, cmd, &p, &feed)) < 0)
1206                     break;
1207                 if (opening) {
1208                     /* add in stream list */
1209                     *last_stream = feed;
1210                     last_stream = &feed->next;
1211                     /* add in feed list */
1212                     *last_feed = feed;
1213                     last_feed = &feed->next_feed;
1214                 }
1215             }
1216         } else if (stream || !av_strcasecmp(cmd, "<Stream")) {
1217             int opening = !av_strcasecmp(cmd, "<Stream");
1218             if (opening && (stream || feed || redirect)) {
1219                 ERROR("Already in a tag\n");
1220             } else {
1221                 if ((ret = ffserver_parse_config_stream(config, cmd, &p, &stream)) < 0)
1222                     break;
1223                 if (opening) {
1224                     /* add in stream list */
1225                     *last_stream = stream;
1226                     last_stream = &stream->next;
1227                 }
1228             }
1229         } else if (redirect || !av_strcasecmp(cmd, "<Redirect")) {
1230             int opening = !av_strcasecmp(cmd, "<Redirect");
1231             if (opening && (stream || feed || redirect))
1232                 ERROR("Already in a tag\n");
1233             else {
1234                 if ((ret = ffserver_parse_config_redirect(config, cmd, &p, &redirect)) < 0)
1235                     break;
1236                 if (opening) {
1237                     /* add in stream list */
1238                     *last_stream = redirect;
1239                     last_stream = &redirect->next;
1240                 }
1241             }
1242         } else {
1243             ffserver_parse_config_global(config, cmd, &p);
1244         }
1245     }
1246     if (stream || feed || redirect)
1247         ERROR("Not closed tag %s\n", stream ? "<Stream>" : (feed ? "<Feed>" : "<Redirect>"));
1248
1249     fclose(f);
1250     if (ret < 0)
1251         return ret;
1252     if (config->errors)
1253         return AVERROR(EINVAL);
1254     else
1255         return 0;
1256 }
1257
1258 #undef ERROR
1259 #undef WARNING
1260
1261 void ffserver_free_child_args(void *argsp)
1262 {
1263     int i;
1264     char **args;
1265     if (!argsp)
1266         return;
1267     args = *(char ***)argsp;
1268     if (!args)
1269         return;
1270     for (i = 0; i < MAX_CHILD_ARGS; i++)
1271         av_free(args[i]);
1272     av_freep(argsp);
1273 }