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