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