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