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