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