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