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