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