OSDN Git Service

Merge commit 'ba2e07909b848e583245856a59d0fe1dd08f917e'
[android-x86/external-ffmpeg.git] / 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, "ab", NULL, 0)) {
234             av->bit_rate = 64000;
235             av_dict_set_int(&recommended, "ab", 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, "mjpeg");
867             stream->fmt = ffserver_guess_format(arg, NULL, NULL);
868             if (!stream->fmt)
869                 ERROR("Unknown Format: '%s'\n", arg);
870         }
871         if (stream->fmt) {
872             config->guessed_audio_codec_id = stream->fmt->audio_codec;
873             config->guessed_video_codec_id = stream->fmt->video_codec;
874         }
875     } else if (!av_strcasecmp(cmd, "InputFormat")) {
876         ffserver_get_arg(arg, sizeof(arg), p);
877         stream->ifmt = av_find_input_format(arg);
878         if (!stream->ifmt)
879             ERROR("Unknown input format: '%s'\n", arg);
880     } else if (!av_strcasecmp(cmd, "FaviconURL")) {
881         if (stream->stream_type == STREAM_TYPE_STATUS)
882             ffserver_get_arg(stream->feed_filename,
883                     sizeof(stream->feed_filename), p);
884         else
885             ERROR("FaviconURL only permitted for status streams\n");
886     } else if (!av_strcasecmp(cmd, "Author")    ||
887                !av_strcasecmp(cmd, "Comment")   ||
888                !av_strcasecmp(cmd, "Copyright") ||
889                !av_strcasecmp(cmd, "Title")) {
890         char key[32];
891         int i;
892         ffserver_get_arg(arg, sizeof(arg), p);
893         for (i = 0; i < strlen(cmd); i++)
894             key[i] = av_tolower(cmd[i]);
895         key[i] = 0;
896         WARNING("Deprecated '%s' option in configuration file. Use "
897                 "'Metadata %s VALUE' instead.\n", cmd, key);
898         if (av_dict_set(&stream->metadata, key, arg, 0) < 0)
899             goto nomem;
900     } else if (!av_strcasecmp(cmd, "Metadata")) {
901         ffserver_get_arg(arg, sizeof(arg), p);
902         ffserver_get_arg(arg2, sizeof(arg2), p);
903         if (av_dict_set(&stream->metadata, arg, arg2, 0) < 0)
904             goto nomem;
905     } else if (!av_strcasecmp(cmd, "Preroll")) {
906         ffserver_get_arg(arg, sizeof(arg), p);
907         stream->prebuffer = atof(arg) * 1000;
908     } else if (!av_strcasecmp(cmd, "StartSendOnKey")) {
909         stream->send_on_key = 1;
910     } else if (!av_strcasecmp(cmd, "AudioCodec")) {
911         ffserver_get_arg(arg, sizeof(arg), p);
912         ffserver_set_codec(config->dummy_actx, arg, config);
913     } else if (!av_strcasecmp(cmd, "VideoCodec")) {
914         ffserver_get_arg(arg, sizeof(arg), p);
915         ffserver_set_codec(config->dummy_vctx, arg, config);
916     } else if (!av_strcasecmp(cmd, "MaxTime")) {
917         ffserver_get_arg(arg, sizeof(arg), p);
918         stream->max_time = atof(arg) * 1000;
919     } else if (!av_strcasecmp(cmd, "AudioBitRate")) {
920         float f;
921         ffserver_get_arg(arg, sizeof(arg), p);
922         ffserver_set_float_param(&f, arg, 1000, -FLT_MAX, FLT_MAX, config,
923                 "Invalid %s: '%s'\n", cmd, arg);
924         if (ffserver_save_avoption_int("ab", (int64_t)lrintf(f),
925                                        AV_OPT_FLAG_AUDIO_PARAM, config) < 0)
926             goto nomem;
927     } else if (!av_strcasecmp(cmd, "AudioChannels")) {
928         ffserver_get_arg(arg, sizeof(arg), p);
929         if (ffserver_save_avoption("ac", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0)
930             goto nomem;
931     } else if (!av_strcasecmp(cmd, "AudioSampleRate")) {
932         ffserver_get_arg(arg, sizeof(arg), p);
933         if (ffserver_save_avoption("ar", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0)
934             goto nomem;
935     } else if (!av_strcasecmp(cmd, "VideoBitRateRange")) {
936         int minrate, maxrate;
937         char *dash;
938         ffserver_get_arg(arg, sizeof(arg), p);
939         dash = strchr(arg, '-');
940         if (dash) {
941             *dash = '\0';
942             dash++;
943             if (ffserver_set_int_param(&minrate, arg,  1000, 0, INT_MAX, config, "Invalid %s: '%s'", cmd, arg) >= 0 &&
944                 ffserver_set_int_param(&maxrate, dash, 1000, 0, INT_MAX, config, "Invalid %s: '%s'", cmd, arg) >= 0) {
945                 if (ffserver_save_avoption_int("minrate", minrate, AV_OPT_FLAG_VIDEO_PARAM, config) < 0 ||
946                     ffserver_save_avoption_int("maxrate", maxrate, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
947                 goto nomem;
948             }
949         } else
950             ERROR("Incorrect format for VideoBitRateRange. It should be "
951                   "<min>-<max>: '%s'.\n", arg);
952     } else if (!av_strcasecmp(cmd, "Debug")) {
953         ffserver_get_arg(arg, sizeof(arg), p);
954         if (ffserver_save_avoption("debug", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0 ||
955             ffserver_save_avoption("debug", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
956             goto nomem;
957     } else if (!av_strcasecmp(cmd, "Strict")) {
958         ffserver_get_arg(arg, sizeof(arg), p);
959         if (ffserver_save_avoption("strict", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0 ||
960             ffserver_save_avoption("strict", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
961             goto nomem;
962     } else if (!av_strcasecmp(cmd, "VideoBufferSize")) {
963         ffserver_get_arg(arg, sizeof(arg), p);
964         ffserver_set_int_param(&val, arg, 8*1024, 0, INT_MAX, config,
965                 "Invalid %s: '%s'", cmd, arg);
966         if (ffserver_save_avoption_int("bufsize", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
967             goto nomem;
968     } else if (!av_strcasecmp(cmd, "VideoBitRateTolerance")) {
969         ffserver_get_arg(arg, sizeof(arg), p);
970         ffserver_set_int_param(&val, arg, 1000, INT_MIN, INT_MAX, config,
971                                "Invalid %s: '%s'", cmd, arg);
972         if (ffserver_save_avoption_int("bt", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
973             goto nomem;
974     } else if (!av_strcasecmp(cmd, "VideoBitRate")) {
975         ffserver_get_arg(arg, sizeof(arg), p);
976         ffserver_set_int_param(&val, arg, 1000, INT_MIN, INT_MAX, config,
977                                "Invalid %s: '%s'", cmd, arg);
978         if (ffserver_save_avoption_int("b", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
979            goto nomem;
980     } else if (!av_strcasecmp(cmd, "VideoSize")) {
981         int ret, w, h;
982         ffserver_get_arg(arg, sizeof(arg), p);
983         ret = av_parse_video_size(&w, &h, arg);
984         if (ret < 0)
985             ERROR("Invalid video size '%s'\n", arg);
986         else {
987             if (w % 2 || h % 2)
988                 WARNING("Image size is not a multiple of 2\n");
989             if (ffserver_save_avoption("video_size", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
990                 goto nomem;
991         }
992     } else if (!av_strcasecmp(cmd, "VideoFrameRate")) {
993         ffserver_get_arg(&arg[2], sizeof(arg) - 2, p);
994         arg[0] = '1'; arg[1] = '/';
995         if (ffserver_save_avoption("time_base", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
996             goto nomem;
997     } else if (!av_strcasecmp(cmd, "PixelFormat")) {
998         enum AVPixelFormat pix_fmt;
999         ffserver_get_arg(arg, sizeof(arg), p);
1000         pix_fmt = av_get_pix_fmt(arg);
1001         if (pix_fmt == AV_PIX_FMT_NONE)
1002             ERROR("Unknown pixel format: '%s'\n", arg);
1003         else if (ffserver_save_avoption("pixel_format", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1004             goto nomem;
1005     } else if (!av_strcasecmp(cmd, "VideoGopSize")) {
1006         ffserver_get_arg(arg, sizeof(arg), p);
1007         if (ffserver_save_avoption("g", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1008             goto nomem;
1009     } else if (!av_strcasecmp(cmd, "VideoIntraOnly")) {
1010         if (ffserver_save_avoption("g", "1", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1011             goto nomem;
1012     } else if (!av_strcasecmp(cmd, "VideoHighQuality")) {
1013         if (ffserver_save_avoption("mbd", "+bits", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1014             goto nomem;
1015     } else if (!av_strcasecmp(cmd, "Video4MotionVector")) {
1016         if (ffserver_save_avoption("mbd", "+bits",  AV_OPT_FLAG_VIDEO_PARAM, config) < 0 || //FIXME remove
1017             ffserver_save_avoption("flags", "+mv4", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1018             goto nomem;
1019     } else if (!av_strcasecmp(cmd, "AVOptionVideo") ||
1020                !av_strcasecmp(cmd, "AVOptionAudio")) {
1021         int ret;
1022         ffserver_get_arg(arg, sizeof(arg), p);
1023         ffserver_get_arg(arg2, sizeof(arg2), p);
1024         if (!av_strcasecmp(cmd, "AVOptionVideo"))
1025             ret = ffserver_save_avoption(arg, arg2, AV_OPT_FLAG_VIDEO_PARAM,
1026                                          config);
1027         else
1028             ret = ffserver_save_avoption(arg, arg2, AV_OPT_FLAG_AUDIO_PARAM,
1029                                          config);
1030         if (ret < 0)
1031             goto nomem;
1032     } else if (!av_strcasecmp(cmd, "AVPresetVideo") ||
1033                !av_strcasecmp(cmd, "AVPresetAudio")) {
1034         ffserver_get_arg(arg, sizeof(arg), p);
1035         if (!av_strcasecmp(cmd, "AVPresetVideo"))
1036             ffserver_opt_preset(arg, AV_OPT_FLAG_VIDEO_PARAM, config);
1037         else
1038             ffserver_opt_preset(arg, AV_OPT_FLAG_AUDIO_PARAM, config);
1039     } else if (!av_strcasecmp(cmd, "VideoTag")) {
1040         ffserver_get_arg(arg, sizeof(arg), p);
1041         if (strlen(arg) == 4 &&
1042             ffserver_save_avoption_int("codec_tag",
1043                                        MKTAG(arg[0], arg[1], arg[2], arg[3]),
1044                                        AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1045             goto nomem;
1046     } else if (!av_strcasecmp(cmd, "BitExact")) {
1047         if (ffserver_save_avoption("flags", "+bitexact", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1048             goto nomem;
1049     } else if (!av_strcasecmp(cmd, "DctFastint")) {
1050         if (ffserver_save_avoption("dct", "fastint", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1051             goto nomem;
1052     } else if (!av_strcasecmp(cmd, "IdctSimple")) {
1053         if (ffserver_save_avoption("idct", "simple", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1054             goto nomem;
1055     } else if (!av_strcasecmp(cmd, "Qscale")) {
1056         ffserver_get_arg(arg, sizeof(arg), p);
1057         ffserver_set_int_param(&val, arg, 0, INT_MIN, INT_MAX, config,
1058                                "Invalid Qscale: '%s'\n", arg);
1059         if (ffserver_save_avoption("flags", "+qscale", AV_OPT_FLAG_VIDEO_PARAM, config) < 0 ||
1060             ffserver_save_avoption_int("global_quality", FF_QP2LAMBDA * val,
1061                                        AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1062             goto nomem;
1063     } else if (!av_strcasecmp(cmd, "VideoQDiff")) {
1064         ffserver_get_arg(arg, sizeof(arg), p);
1065         if (ffserver_save_avoption("qdiff", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1066             goto nomem;
1067     } else if (!av_strcasecmp(cmd, "VideoQMax")) {
1068         ffserver_get_arg(arg, sizeof(arg), p);
1069         if (ffserver_save_avoption("qmax", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1070             goto nomem;
1071     } else if (!av_strcasecmp(cmd, "VideoQMin")) {
1072         ffserver_get_arg(arg, sizeof(arg), p);
1073         if (ffserver_save_avoption("qmin", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1074             goto nomem;
1075     } else if (!av_strcasecmp(cmd, "LumiMask")) {
1076         ffserver_get_arg(arg, sizeof(arg), p);
1077         if (ffserver_save_avoption("lumi_mask", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1078             goto nomem;
1079     } else if (!av_strcasecmp(cmd, "DarkMask")) {
1080         ffserver_get_arg(arg, sizeof(arg), p);
1081         if (ffserver_save_avoption("dark_mask", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1082             goto nomem;
1083     } else if (!av_strcasecmp(cmd, "NoVideo")) {
1084         config->no_video = 1;
1085     } else if (!av_strcasecmp(cmd, "NoAudio")) {
1086         config->no_audio = 1;
1087     } else if (!av_strcasecmp(cmd, "ACL")) {
1088         ffserver_parse_acl_row(stream, NULL, NULL, *p, config->filename,
1089                 config->line_num);
1090     } else if (!av_strcasecmp(cmd, "DynamicACL")) {
1091         ffserver_get_arg(stream->dynamic_acl, sizeof(stream->dynamic_acl), p);
1092     } else if (!av_strcasecmp(cmd, "RTSPOption")) {
1093         ffserver_get_arg(arg, sizeof(arg), p);
1094         av_freep(&stream->rtsp_option);
1095         stream->rtsp_option = av_strdup(arg);
1096     } else if (!av_strcasecmp(cmd, "MulticastAddress")) {
1097         ffserver_get_arg(arg, sizeof(arg), p);
1098         if (resolve_host(&stream->multicast_ip, arg))
1099             ERROR("Invalid host/IP address: '%s'\n", arg);
1100         stream->is_multicast = 1;
1101         stream->loop = 1; /* default is looping */
1102     } else if (!av_strcasecmp(cmd, "MulticastPort")) {
1103         ffserver_get_arg(arg, sizeof(arg), p);
1104         ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
1105                 "Invalid MulticastPort: '%s'\n", arg);
1106         stream->multicast_port = val;
1107     } else if (!av_strcasecmp(cmd, "MulticastTTL")) {
1108         ffserver_get_arg(arg, sizeof(arg), p);
1109         ffserver_set_int_param(&val, arg, 0, INT_MIN, INT_MAX, config,
1110                 "Invalid MulticastTTL: '%s'\n", arg);
1111         stream->multicast_ttl = val;
1112     } else if (!av_strcasecmp(cmd, "NoLoop")) {
1113         stream->loop = 0;
1114     } else if (!av_strcasecmp(cmd, "</Stream>")) {
1115         config->stream_use_defaults &= 1;
1116         if (stream->feed && stream->fmt && strcmp(stream->fmt->name, "ffm")) {
1117             if (config->dummy_actx->codec_id == AV_CODEC_ID_NONE)
1118                 config->dummy_actx->codec_id = config->guessed_audio_codec_id;
1119             if (!config->no_audio &&
1120                 config->dummy_actx->codec_id != AV_CODEC_ID_NONE) {
1121                 AVCodecContext *audio_enc = avcodec_alloc_context3(avcodec_find_encoder(config->dummy_actx->codec_id));
1122                 add_codec(stream, audio_enc, config);
1123             }
1124             if (config->dummy_vctx->codec_id == AV_CODEC_ID_NONE)
1125                 config->dummy_vctx->codec_id = config->guessed_video_codec_id;
1126             if (!config->no_video &&
1127                 config->dummy_vctx->codec_id != AV_CODEC_ID_NONE) {
1128                 AVCodecContext *video_enc = avcodec_alloc_context3(avcodec_find_encoder(config->dummy_vctx->codec_id));
1129                 add_codec(stream, video_enc, config);
1130             }
1131         }
1132         av_dict_free(&config->video_opts);
1133         av_dict_free(&config->audio_opts);
1134         avcodec_free_context(&config->dummy_vctx);
1135         avcodec_free_context(&config->dummy_actx);
1136         *pstream = NULL;
1137     } else if (!av_strcasecmp(cmd, "File") ||
1138                !av_strcasecmp(cmd, "ReadOnlyFile")) {
1139         ffserver_get_arg(stream->feed_filename, sizeof(stream->feed_filename),
1140                 p);
1141     } else if (!av_strcasecmp(cmd, "UseDefaults")) {
1142         if (config->stream_use_defaults > 1)
1143             WARNING("Multiple UseDefaults/NoDefaults entries.\n");
1144         config->stream_use_defaults = 3;
1145     } else if (!av_strcasecmp(cmd, "NoDefaults")) {
1146         if (config->stream_use_defaults > 1)
1147             WARNING("Multiple UseDefaults/NoDefaults entries.\n");
1148         config->stream_use_defaults = 2;
1149     } else {
1150         ERROR("Invalid entry '%s' inside <Stream></Stream>\n", cmd);
1151     }
1152     return 0;
1153   nomem:
1154     av_log(NULL, AV_LOG_ERROR, "Out of memory. Aborting.\n");
1155     av_dict_free(&config->video_opts);
1156     av_dict_free(&config->audio_opts);
1157     avcodec_free_context(&config->dummy_vctx);
1158     avcodec_free_context(&config->dummy_actx);
1159     return AVERROR(ENOMEM);
1160 }
1161
1162 static int ffserver_parse_config_redirect(FFServerConfig *config,
1163                                           const char *cmd, const char **p,
1164                                           FFServerStream **predirect)
1165 {
1166     FFServerStream *redirect;
1167     av_assert0(predirect);
1168     redirect = *predirect;
1169
1170     if (!av_strcasecmp(cmd, "<Redirect")) {
1171         char *q;
1172         redirect = av_mallocz(sizeof(FFServerStream));
1173         if (!redirect)
1174             return AVERROR(ENOMEM);
1175
1176         ffserver_get_arg(redirect->filename, sizeof(redirect->filename), p);
1177         q = strrchr(redirect->filename, '>');
1178         if (*q)
1179             *q = '\0';
1180         redirect->stream_type = STREAM_TYPE_REDIRECT;
1181         *predirect = redirect;
1182         return 0;
1183     }
1184     av_assert0(redirect);
1185     if (!av_strcasecmp(cmd, "URL")) {
1186         ffserver_get_arg(redirect->feed_filename,
1187                 sizeof(redirect->feed_filename), p);
1188     } else if (!av_strcasecmp(cmd, "</Redirect>")) {
1189         if (!redirect->feed_filename[0])
1190             ERROR("No URL found for <Redirect>\n");
1191         *predirect = NULL;
1192     } else {
1193         ERROR("Invalid entry '%s' inside <Redirect></Redirect>\n", cmd);
1194     }
1195     return 0;
1196 }
1197
1198 int ffserver_parse_ffconfig(const char *filename, FFServerConfig *config)
1199 {
1200     FILE *f;
1201     char line[1024];
1202     char cmd[64];
1203     const char *p;
1204     FFServerStream **last_stream, *stream = NULL, *redirect = NULL;
1205     FFServerStream **last_feed, *feed = NULL;
1206     int ret = 0;
1207
1208     av_assert0(config);
1209
1210     f = fopen(filename, "r");
1211     if (!f) {
1212         ret = AVERROR(errno);
1213         av_log(NULL, AV_LOG_ERROR,
1214                 "Could not open the configuration file '%s'\n", filename);
1215         return ret;
1216     }
1217
1218     config->first_stream = NULL;
1219     config->first_feed = NULL;
1220     config->errors = config->warnings = 0;
1221
1222     last_stream = &config->first_stream;
1223     last_feed = &config->first_feed;
1224
1225     config->line_num = 0;
1226     while (fgets(line, sizeof(line), f) != NULL) {
1227         config->line_num++;
1228         p = line;
1229         while (av_isspace(*p))
1230             p++;
1231         if (*p == '\0' || *p == '#')
1232             continue;
1233
1234         ffserver_get_arg(cmd, sizeof(cmd), &p);
1235
1236         if (feed || !av_strcasecmp(cmd, "<Feed")) {
1237             int opening = !av_strcasecmp(cmd, "<Feed");
1238             if (opening && (stream || feed || redirect)) {
1239                 ERROR("Already in a tag\n");
1240             } else {
1241                 ret = ffserver_parse_config_feed(config, cmd, &p, &feed);
1242                 if (ret < 0)
1243                     break;
1244                 if (opening) {
1245                     /* add in stream & feed list */
1246                     *last_stream = feed;
1247                     *last_feed = feed;
1248                     last_stream = &feed->next;
1249                     last_feed = &feed->next_feed;
1250                 }
1251             }
1252         } else if (stream || !av_strcasecmp(cmd, "<Stream")) {
1253             int opening = !av_strcasecmp(cmd, "<Stream");
1254             if (opening && (stream || feed || redirect)) {
1255                 ERROR("Already in a tag\n");
1256             } else {
1257                 ret = ffserver_parse_config_stream(config, cmd, &p, &stream);
1258                 if (ret < 0)
1259                     break;
1260                 if (opening) {
1261                     /* add in stream list */
1262                     *last_stream = stream;
1263                     last_stream = &stream->next;
1264                 }
1265             }
1266         } else if (redirect || !av_strcasecmp(cmd, "<Redirect")) {
1267             int opening = !av_strcasecmp(cmd, "<Redirect");
1268             if (opening && (stream || feed || redirect))
1269                 ERROR("Already in a tag\n");
1270             else {
1271                 ret = ffserver_parse_config_redirect(config, cmd, &p,
1272                                                      &redirect);
1273                 if (ret < 0)
1274                     break;
1275                 if (opening) {
1276                     /* add in stream list */
1277                     *last_stream = redirect;
1278                     last_stream = &redirect->next;
1279                 }
1280             }
1281         } else {
1282             ffserver_parse_config_global(config, cmd, &p);
1283         }
1284     }
1285     if (stream || feed || redirect)
1286         ERROR("Missing closing </%s> tag\n",
1287               stream ? "Stream" : (feed ? "Feed" : "Redirect"));
1288
1289     fclose(f);
1290     if (ret < 0)
1291         return ret;
1292     if (config->errors)
1293         return AVERROR(EINVAL);
1294     else
1295         return 0;
1296 }
1297
1298 #undef ERROR
1299 #undef WARNING
1300
1301 void ffserver_free_child_args(void *argsp)
1302 {
1303     int i;
1304     char **args;
1305     if (!argsp)
1306         return;
1307     args = *(char ***)argsp;
1308     if (!args)
1309         return;
1310     for (i = 0; i < MAX_CHILD_ARGS; i++)
1311         av_free(args[i]);
1312     av_freep(argsp);
1313 }